Search code examples
pythonfor-loopdataframefile-permissions

How to open the .txt in a folder if the .xls in another folder is of same name


There are two folders containing individual's data, one folder containing .txt files, and another folder containing the .xls files.

Folder1 :['A.txt', 'B.txt', 'C.txt']
Folder2 :['A.xls', 'B.xls', 'C.xls']

I want to open both the files of the same individual and do some action on .txt file based on information in .xls file.

This is what i tried:

Tpath = 'Path to Folder1'
Xpath = 'Path to Folder2'
for tfilename in glob.glob(os.path.join(Tpath)):
for tfile in os.listdir(Tpath):
    for xfilename in glob.glob(os.path.join(Xpath)):
        for xfile in os.listdir(Xpath):
            if (xfile[:-4] == tfile[:-4]):
                tdf = pd.read_csv(tfilename)
                xdf = pd.read_excel(xfilename)      

I encounter Permission Denied error. I know it is due to the fact that I am trying to access the file from the for loop from one folder to for loop from another folder.

But I don't know how to solve this. Can anyone correct my mistake here?


Solution

  • You are getting permission error because tfilename and xfilename end up being the path to the directory, not the file. You can verify this by using print instead of read_csv and read_excel:

    Tpath = r'\some\path\txt'
    Xpath = r'\some\path\xls'
    for tfilename in glob.glob(os.path.join(Tpath)):
        for tfile in os.listdir(Tpath):
            for xfilename in glob.glob(os.path.join(Xpath)):
                for xfile in os.listdir(Xpath):
                    if (xfile[:-4] == tfile[:-4]):
                        print(tfilename)
                        print(xfilename)
    

    Outputs

    '\some\path\txt'
    '\some\path\xls'
    

    Before opening the files you will have to join the file names to the paths, ie

    tdf = pd.read_csv(os.path.join(tfilename, tfile))
    xdf = pd.read_excel(os.path.join(xfilename, xfile))
    

    At this point you may want to change the names of tfilename and xfilename so the whole code will make more sense.

    Also, I'm not sure why your code calls os.path.join with a single argument. It has no effect whatsoever.