I need to read all files in a directory and save into list, then read list those files one by one. I don't want to use external module like 'glob module'. So, trying through 2 different approach: First approach:
import os file_list = os.listdir("jsons")
for files in file_list: data = open(files,"r")
output:
['A03DUrQz1BM9SQ2.json', 'A04D5V1u1BMxaV6.json', 'A0kxiHL81AN9pH5.json', 'A1Fxs5Ag1A8vuB5.json', 'A2Dsv7RE1BDqYt5.json', 'A2HkZPkn1BpvvG5.json']
but here issue is that filenames are saved in string format and not able to open this file as it read it with quotes ''.
2nd approach:
file_list = os.system("ls jsons/") print file_list.split() for files in file_list: data = open(files,"r") print data
output:
Traceback (most recent call last): File "asn-1_q3.py", line 9, in <module> print file_list.split() AttributeError: 'int' object has no attribute 'split' Here, it is saving as int and not able to split the file. How should I solve them ?
No responses yet.