Lieutenant Dan
Lieutenant Dan

Reputation: 8284

(Rename current file with varirable) FileNotFoundError: [WinError 2] The system cannot find the file specified:

In the below; I believe I am doing the os.rename incorrectly. In the console it's finding and printing the right contents; but it is not doing the renaming.

What I really want to do is rename the current file, with the MemberI value, then move to a new directory. But at this point I'll settle to rename as is. MemberI is reporting accurately in console, just can't get the rename to perform. Any ideas.

for filename in os.listdir(config.Total):
    if filename.endswith(".pdf"):
        First_Name, Last_Name, Zip = filename.replace(".pdf",'').split()
        Name = First_Name + " " + Last_Name

        print(Name)
        print(Zip)
        data1 = pd.read_excel(config.Excel1)
        data2 = pd.read_excel(config.Excel2)

        df = pd.DataFrame(data1)
        header = df.iloc[0]
        df2 = pd.DataFrame(data2)
        header2 = df2.iloc[0]

        df = df[1:]
        df.rename(columns = header)
        df2 = df2[1:]
        df2.rename(columns = header2)

        row_numberd1 = df[df['Member Name'].str.contains(Name)].index.min()
        row_numberd12 = df[df['Member Address Line 3'].str.contains(Zip)].index.min()

        if row_numberd1 == row_numberd12: # When rows match of NameUp and Zip var in DF1
            rowMatched = row_numberd1
            print("Match Found")
            print(rowMatched)

            MemberID = df['ID'][rowMatched]
            MemberI = str(MemberID)

            os.rename(filename, MemberI)

Upvotes: 1

Views: 63

Answers (1)

Manvir
Manvir

Reputation: 779

You need the full path of the file that you're trying to rename filename only has the name of the file not the entire path

os.rename(config.Total+'/'+filename, MemberI)

if config.Total already has / then just

os.rename(config.Total+filename, MemberI)

Upvotes: 1

Related Questions