I am a total Python Newb
I need to loop through a directory looking for .txt files, and then read and process them individually. I would like to set this up so that whatever directory the script is in is treated as the root of this action. For example if the script is in /bsepath/workDir, then it would loop over all of the files in workDir and its children.
What I have so far is:
#!/usr/bin/env python import os scrptPth = os.path.realpath(__file__) for file in os.listdir(scrptPth) with open(file) as f: head,sub,auth = [f.readline().strip() for i in range(3)] data=f.read() #data.encode('utf-8') pth = os.getcwd() print head,sub,auth,data,pth
This code is giving me an invalid syntax error and I suspect that is because
os.listdir does not like file paths in standard string format. Also I dont think that I am doing the looped action right. How do I reference a specific file in the looped action? Is it packaged as a variable?
Any help is appriciated
import os, fnmatch def findFiles (path, filter): for root, dirs, files in os.walk(path): for file in fnmatch.filter(files, filter): yield os.path.join(root, file)
Use it like this, and it will find all text files somewhere within the given path (recursively):
for textFile in findFiles(r'C:\Users\poke\Documents', '*.txt'): print(textFile)
os.listdir expects a directory as input. So, to get the directory in which the script resides use:
scrptPth = os.path.dirname(os.path.realpath(__file__))<hr>
os.listdir returns just the filenames, not the full path.
open(file) will not work unless the current working directory happens to be the directory where the script resides. To fix this, use
import os scrptPth = os.path.dirname(os.path.realpath(__file__)) for file in os.listdir(scrptPth): with open(os.path.join(scrptPth, file)) as f:<hr>
Finally, if you want to recurse through subdirectories, use
import os scrptPth = os.path.dirname(os.path.realpath(__file__)) for root, dirs, files in os.walk(scrptPth): for filename in files: filename = os.path.join(root, filename) with open(filename, 'r') as f: head,sub,auth = [f.readline().strip() for i in range(3)] data=f.read() #data.encode('utf-8')