1

There's a directory : /home/zurelsoft/files with the file names .sachitadhFebruary28,2013,18:45PMsolexa.zip.M9mw9e

There are lots of files like this:

.sachitadhFebruary28,2013,18:45PMsolexa.zip.M
.sachitadhFebruary28,2013,18:45PMsolexa.zip.KK

which are stored in a database.

    current_file = Queue.objects.all()
      j = [i.file_session for i in current_file]
        k = [str(i) for i in j]
        new_file_size = [int(os.path.getsize(i+'*')) for i in k]

I am trying to get the filsize like this but I am getting No such directory error.

I found glob.glob() used that but didn't work. How can I do this?

pynovice
  • 7,424
  • 25
  • 69
  • 109
  • /home/zurelsoft/files is a directory name and all those files are under it. See my updated question. – pynovice Feb 28 '13 at 13:18
  • 1
    "I found `glob.glob()` used that but didn't work." The only answer for "didn't work" is "do it right". It has the same level of detail... What did not work? – glglgl Feb 28 '13 at 13:26

1 Answers1

3

getsize takes exactly one filename - and anything you give it is assumed to be exactly a filename. To get the expansion you want, you do want to use glob.glob - presumably, it didn't work because you did something like:

os.path.getsize(glob.glob('/home/zurelsoft/*'))

Which will always give an exception, since glob doesn't return the exactly one filename that getsize expects - it returns a list of all the matches. Iterate over that list to get each individual one - eg,

for filename in glob.glob('/home/zurelsoft/*'):
   print(os.path.getsize(filename))

will print the size of every file in your home directory.

lvc
  • 34,233
  • 10
  • 73
  • 98
  • I have list as a argument for glob.glob()? And it's not working. – pynovice Mar 01 '13 at 03:59
  • @user2032220 it is the same pattern again: `glob` takes a *single* pattern (as a string), and gives you back a list of all the filenames that match it. If you have a list of patterns that you want to resolve, you'll need to pass each element individually to `glob` by iterating over it. – lvc Mar 01 '13 at 04:05
  • Yeah done but I want the answer in list comprehension: Can you please answer my question: http://stackoverflow.com/questions/15150458/how-to-convert-this-for-loop-in-list-comprehension – pynovice Mar 01 '13 at 04:25