Reputation: 43
I have many documents in a folder. They in thousands in a .txt format. I want to read all of those documents and want to store documents content in a dictionary. The key of dictionary can be documents name and value should be content of that particular document. But right now I am only able to print all the names of my files but not able to make a dictionary where its content can be stored. I have written this code:
import os
with os.scandir('docs/') as entries:
for entry in entries:
print(entry.name)
with open('F:\folder\docs\'+entry.name) as f:
contents = {'entry.name': f.read()}
Or if you can give me some new method to read content from all the files and then store it in a dictionary. Please help me in this regard. Thank you.
Upvotes: 1
Views: 791
Reputation: 208
This way it should work.
import os
docs_dict = {}
with os.scandir('docs/') as entries:
for entry in entries:
print(entry.name)
with open('F:/folder/docs/'+ entry.name ) as f:
val = f.read()
docs_dict[f.name] = val
f.close()
I created an empty dictionary outside the for
loop. Then, expanding your code, I stored the content of the document in val
and then added the dictionary item. I also changed F:\folder\docs\
to F:/folder/docs/
, cause Python wants forward slashes in paths, and using \
excludes the ending quote of the string.
Upvotes: 1
Reputation: 6156
my solution:
import os
import json
all_files = dict()
with os.scandir() as entries:
for entry in entries:
print(entry.name)
with open(rf'{entry.name}') as f:
contents = f.read()
all_files[f'{entry.name}'] = contents
with open('all_files.json', 'w') as f:
json.dump(all_files, f, indent=2)
Upvotes: 0
Reputation: 23815
assuming the loop in the code you have posted works - the code below should create the dict you are looking for.
import os
data = {}
with os.scandir('docs/') as entries:
for entry in entries:
with open('F:\folder\docs\'+entry.name) as f:
data[entry.name] = f.read()
Upvotes: 0