Save output of a script in txt (Windows)

I created a script to see all the files in a folder and print the full path of each file. The script is working and prints the output in the Command Prompt (Windows)

import os

root = 'C:\Users\marco\Desktop\Folder'

for path, subdirs, files in os.walk(root):
    for name in files:
        print os.path.join(path, name)

I want now to save the output in a txt file so I have edited the code assigning the os.path.join(path,name) to a variable values but, when I print the output, the script gives me an error

import os

root = 'C:\Users\marco\Desktop\Folder'

for path, subdirs, files in os.walk(root):
    for name in files:
        values = os.path.join(path, name)

file = open('sample.txt', 'w')
file.write(values)
file.close()    

Error below

file.write(values)
NameError: name 'values' is not defined

Upvotes: 1

Views: 395

Answers (3)

Souyama
Souyama

Reputation: 116

The problem is the variable values is only limited to the scope of the inner for loop. So assign empty value to the variable before you start the iteration. Like values=None or better yet values='' Now assuming the above code even worked you wouldn't get the output file you desired. You see, the variable values is being regularly updated. So after the iteration the location of the last file encountered would be stored in values which would then be written in the sample.txt file.

Another bad practice you seem to be following is using \ instead of \\ inside strings. This might come to bite you later (if they haven't already). You see \ when followed by a letter denotes an escape sequence\character and \\ is the escape sequence for slash.

So here's a redesigned working sample code:

import os
root = 'C:\\Users\\marco\\Desktop\\Folder'
values=''

for path, subdirs, files in os.walk(root):
    for name in files:
        values = values + os.path.join(path, name) + '\n'

samplef = open('sample.txt', 'w')
samplef.write(values)
samplef.close()    

In case you aren't familiar, '\n' denotes the escape sequence for a new-line. Reading your output file would be quite tedious had all your files been written been on the same line.

PS: I did the code with stings as that's what I would prefer in this scenario, but you way try arrays or lists or what-not. Just be sure that you define the variable beforehand lest you should get out of scope.

Upvotes: 1

goodvibration
goodvibration

Reputation: 6206

Try this:

file = open('sample.txt', 'w')
for path, subdirs, files in os.walk(root):
    for name in files:
        values = os.path.join(path, name)
        file.write(values+'\n')
file.close()

Note that file is a builtin symbol in Python (which is overridden here), so I suggest that you replace it with fileDesc or similar.

Upvotes: 3

NishanthSpShetty
NishanthSpShetty

Reputation: 659

import os

root = 'C:\Users\marco\Desktop\Folder'
file = open('sample.txt', 'w')
for path, subdirs, files in os.walk(root):
    for name in files:
       values = os.path.join(path, name)
       file.write(values)
file.close()    

values is not defined to be availbale in the lexical scope of the file. It is scoped within a inner loop. change it as above, will work.

Upvotes: 1

Related Questions