Iterating and modifying files in a directory

I already have code that works to modify one .edi file (testedifact.edi) in the same directory as my program. however I need to run my script against a folder containing many of these .edi files so I basically want to use my code to be applied to every single file

here's what I have that works on one file:

segmentsNew = []
global segments     
with open( "testedifact.edi" , "r+") as edifactile:
    segments = edifactile.readlines()
    versionNumber = getVersionNumber(segments)
    for segment in segments:
        #do stuffs
edifactile.close()
with open ("testedifact.edi" , "w") as edifactfile:
    edifactile.writelines(segmentsNew)
edifactfile.close()

but I want to be able to do this for files outside of this directory and also on our network drives..

I've tried iterating through the files in my directory (as a little test) and passing every file to "with open.." like this

directory = os.listdir(r'C:\Users\name\test_edi_dir')
for file in directory:
    print("printing file names:", file)
    with  open(file, 'r') as edifactfile:
        pass

print(edifactfile.closed)

and I keep getting FileNotFoundError: [Errno 2] No such file or directory: 'testedifact - Kopie (10).edi' though it prints the file name.. what am I doing wrong?

could someone help please?

3 answers

  • answered 2020-01-15 09:27 Alexander Pushkarev

    That happens because when you call open(file, 'r') it tries to open a file in the current working directory.

    Change your code to this:

    directory = os.listdir(r'C:\Users\name\test_edi_dir')
    for file in directory:
        print("printing file names:", file)
        with  open('C:\Users\name\test_edi_dir\' + file, 'r') as edifactile:
            pass
    
        print(edifactfile.closed)
    

    The next issue is that some files will be actually a directories, and your code may fail with the following error:

    traceback (most recent call last):
      File "<stdin>", line 3, in <module>
    IOError: [Errno 21] Is a directory: '...'
    

    So you want to check if file is actually a file, before opening it:

    isFile = os.path.isfile('C:\Users\name\test_edi_dir\' + file)
    

    And finally a complete code is:

    directory = os.listdir(r'C:\Users\name\test_edi_dir')
    for file in directory:
        print("printing file names:", file)
        full_filename = 'C:\Users\name\test_edi_dir\' + file
        if os.path.isdir(full_filename):
             continue
        with  open(full_filename, 'r') as edifactile:
            pass
    
        print(edifactfile.closed)
    

  • answered 2020-01-15 09:28 T Piper

    Looks like you have to pass the entire image path:

    with open('C:\Users\name\test_edi_dir\' + file, 'r') as edifactile:
        pass
    

  • answered 2020-01-15 09:29 offeltoffel

    file only contains the file-name, not the path it is stored in. You need to pass this, too.

    path = r'C:\Users\name\test_edi_dir/'
    directory = os.listdir(path)
    
    for file in directory:
        print("printing file names:", file)
        with open(path+file, 'r') as edifactile:
            pass