Federico Sciarretta
Federico Sciarretta

Reputation: 372

List a files in python and save it

I can't save this output, maybe someone have the solution. I'm listing a directory and some singles files. But when I save the output just catch the directory files, and Not the singles files. My code:

import os
tosave = open('/tmp/list','ab')
thesource = ["/etc/ssh","/var/log/syslog","/etc/hosts"]
for f in thesource:
    print f
    for top, dirs, files in os.walk(f):
        for nm in files:
            print os.path.join(top, nm)
            try:
                tosave.write(top+nm+'\n')
            finally:
                tosave.close

I saw in the console all files and directory, but in the saved list, just ssh files. Why didn't save syslog and hosts too?

Thank you !!

Upvotes: 0

Views: 244

Answers (6)

Cwt
Cwt

Reputation: 8576

In case you missed the () at tosave.close while pasting: (otherwise check harsh's answer)

The finally is wrong here. The code in finally will be executed after the try block, so after the first execution of tosave.write(top+nm+'\n') the file will be closed because of tosave.close().

Possibly you intended to use except:

            # snip
            try:
                tosave.write(top+nm+'\n')
            except:
                tosave.close()

Edit: To answer your comment, you want the last line to be the same as the print statement:

                tosave.write(os.path.join(top, nm) + '\n')

Upvotes: 1

Federico Sciarretta
Federico Sciarretta

Reputation: 372

With all your help I found a solution and it's working. I share it.

tosave = open('/tmp/list','ab')
    thesource = ["/etc/ssh","/var/log/syslog","/etc/hosts"]
for f in thesource:
    if os.path.isfile(f):
        print f
        tosave.write(f+'\n')
    else:
        for top, dirs, files in os.walk(f):
            for nm in files:
                print os.path.join(top, nm)
                tosave.write(top+nm+'\n')

Thank you all for your help !!!

Upvotes: 1

Bite code
Bite code

Reputation: 596863

Put the close() at the end of the script.

Otherwise it will close the file after the first step in the loop, making the file unwritable.

Upvotes: 0

Harshniket Seta
Harshniket Seta

Reputation: 654

Check if this works for you

import os
tosave = open('/tmp/list','ab')
thesource = ["/etc/ssh","/var/log/syslog","/etc/hosts"]
for f in thesource:
    if os.path.isdir(f):
        for top, dirs, files in os.walk(f):
            for nm in files:
                try:
                    tosave.write(top+nm+'\n')
    if os.path.isfile(f):
        tosave.write(f+'\n')
to.close()

Upvotes: 1

Divya
Divya

Reputation: 2622

You could try adding tosave.flush() at the end. It does cause problems sometimes. Sometimes, a flush call is required to empty the contents of the buffer into the file.

Upvotes: 1

Eli Bendersky
Eli Bendersky

Reputation: 273566

Maybe this is because you're opening the file in append mode (the 'a') and then looking at its beginning? Look at its end - you might see your new files listed there.

In append mode, each time the script runs it appends its output to the file. Usually what one wants is just the write mode ('w' instead of 'a'), which overwrites the file each time.

Upvotes: 0

Related Questions