0

我正在尝试编写一个将文件名作为输入的代码。在我的电脑上找到这个文件,然后根据文件前 2 行的文本更改文件名。

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)

但是,我WinError 123在尝试运行此代码时得到了。

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'
4

0 回答 0