Ayush Kalani Ayush Kalani - 1 year ago 59
Python Question

OSError file not found when navigating through sub-directories in python?

There is a directory tree structure such that article is main folder and d10x are the subfolders

1.txt 2.txt 3.txt
1.txt 2.txt 3.txt
1.txt 2.txt 3.txt

My program has to go to each of the subfolder and read a text file from it.

My python implementation gets the following error

OSError: [Errno 2] No such file or directory: 'd101'

Python code:

articles = os.listdir("articles")
for yo in articles:
for index in range(len(mo)):
for article in yo:
print 'Reading articles/' + article

OSError: [Errno 2] No such file or directory: 'd101'

Answer Source

You are trying to read d101 instead of articles/d101. Change your listdir() call to this:

os.listdir(os.path.join("articles", mo[index]))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download