KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have the code working to read in the values of a single text file but am having difficulties reading all files from all directories and putting all of the contents together. Here is what I have: filename = '*' filesuffix = '*' location = os.path.join('Test', filename + "." + filesuffix) Document = filename thedictionary = {} with open(location) as f: file_contents = f.read().lower().split(' ') # split line on spaces to make a list for position, item in enumerate(file_contents): if item in thedictionary: thedictionary[item].append(position) else: thedictionary[item] = [position] wordlist = (thedictionary, Document) #print wordlist #print thedictionary note that I am trying to stick the wildcard * in for the filename as well as the wildcard for the filesuffix. I get the following error: "IOError: [Errno 2] No such file or directory: 'Test/ . '" I am not sure if this is even the right way to do it but it seems that if I somehow get the wildcards working - it should work. I have gotten this example to work: Python - reading files from directory file not found in subdirectory (which is there) Which is a little different - but don't know how to update it to read all files. I am thinking that in this initial set of code: previous_dir = os.getcwd() os.chdir('testfilefolder') #add something here? for filename in os.listdir('.'): That I would need to add something where I have an outer for loop but don't quite know what to put in it.. Any thoughts?
Tags (comma-separated)
Save Edits
Cancel