问题描述
我正在尝试编写一个以文件名作为输入的代码。在计算机上找到该文件,然后根据文件的前两行中的文本更改文件名。
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'
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)