loop through folder in python and open files throws an error

后端 未结 2 1345
深忆病人
深忆病人 2021-01-01 00:14

I try to loop through folder and read files but only the first file will open and read correctly but the second files print the name and through an error \"IOError: [Errno 2

相关标签:
2条回答
  • 2021-01-01 00:32

    os.listdir() gives you only the filename, but not the path to the file:

    import os
    
    for filename in os.listdir('path/to/dir'):
        if filename.endswith('.log'):
            with open(os.path.join('path/to/dir', filename)) as f:
                content = f.read()
    

    Alternatively, you could use the glob module. The glob.glob() function allows you to filter files using a pattern:

    import os
    import glob
    
    for filepath in glob.glob(os.path.join('path/to/dir', '*.log')):
        with open(filepath) as f:
            content = f.read()
    
    0 讨论(0)
  • 2021-01-01 00:33

    Using os.listdir(...) only returns the filenames of the directory you passed, but not the full path to the files. You need to include the relative directory path as well when opening the file.

    basepath = "pathtodir/DataFiles/"
    for filename in os.listdir(basepath):
        if filename.endswith(".log"): 
            print(os.path.join("./DataFiles", filename))
    
            with open(basepath + filename) as openfile:    
                for line in openfile:
                ........
    
    0 讨论(0)
提交回复
热议问题