Search code examples
pythonfiledirectoryioerror

help with IOError for reading files


for subdir, dirs, files in os.walk(crawlFolder):
    for file in files:
         print os.getcwd()
         f=open(file,'r')
         lines=f.readlines()
         writeFile.write(lines)
         f.close()
writeFile.close()   

I get the error as:-

IOError: [Errno 2] No such file or directory

In reference to my partial python code above:-

print os.getcwd() --> C:\search engine\taxonomy

however, the file is located in the directory "C:\search engine\taxonomy\testFolder"

I know the error is because it works in the current directory and I need to append the directory testFolder with file somehow. Could someone please correct my code and help me out with this? Thank you.


Solution

  • The subdir variable gives you the path from crawlFolder to the directory containing file, so you just need to pass os.path.join(crawlFolder, subdir, file) to open instead of a bare file. Like so:

    for subdir, dirs, files in os.walk(crawlFolder):
        for file in files:
             print os.getcwd()
             f=open(os.path.join(crawlFolder, subdir, file),'r')
             lines=f.readlines()
             writeFile.write(lines)
             f.close()
    writeFile.close()
    

    Incidentally, this is a more efficient way to copy a file into another file:

    for subdir, dirs, files in os.walk(crawlFolder):
        for file in files:
             print os.getcwd()
             f=open(os.path.join(crawlFolder, subdir, file),'r')
             writeFile.writelines(f)
             f.close()
    writeFile.close()
    

    [EDIT: Can't resist the temptation to play golf:

    for subdir, dirs, files in os.walk(crawlFolder):
        for file in files:
             writeFile.writelines(open(os.path.join(crawlFolder, subdir, file)))
    writeFile.close()
    

    ]