Winerror 123 when trying to rename a file

155 views Asked by At

I'm trying to write a code that takes a file name as an input. Finds this file on my computer and then changes the name of the file according to the text on the 2 first lines of the file.

import os
filename = input("Enter your file name: ")

def info(filename):
    with open(filename, 'r') as filehandle:
        current_line = 1
        for line in filehandle:
            if current_line <=2:
                yield(line)
            current_line += 1
    

info = list(info(filename))
print(info)
path = r'C:\Users\marku\Desktop\INF100'
date = str(info[1])
place = str(info[0])

finalname = date + '_' + place + '.txt'
old = os.path.join(path, filename)
new = os.path.join(path, finalname)

os.rename(old, new)

However, I get WinError 123 when trying to run this code.

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 
  'C:\\Users\\marku\\Desktop\\INF100\\qwerty.txt' -> 'C:\\Users\\marku\\Desktop\\INF100\\2019-06-01\n_Oslo\n.txt'
0

There are 0 answers