我的Python脚本中有两个文件路径列表
old_name = ['/home/student-03-e43755ddd684/data/jane_profile_07272018.doc', '/home/student-03-e43755ddd684/data/jane_contact_07292018.csv']
new_name = ['/home/student-03-e43755ddd684/data/jdoe_profile_07272018.doc', '/home/student-03-e43755ddd684/data/jdoe_contact_07292018.csv']
只是名字jane
被jdoe
改变了
这是我的剧本
#!/usr/bin/env python3
import sys
import subprocess
old_name = ['/home/student-03-e43755ddd684/data/jane_profile_07272018.doc', '/home/student-03-e43755ddd684/data/jane_contact_07292018.csv']
new_name = ['/home/student-03-e43755ddd684/data/jdoe_profile_07272018.doc', '/home/student-03-e43755ddd684/data/jdoe_contact_07292018.csv']
print(old_name)
print(new_name)
for oldname, newname in zip(old_name, new_name):
subprocess.run("mv oldname newname", shell=True)
print("done")
运行之后,它在mv
中给出以下错误
命令:
mv: cannot stat 'oldname': No such file or directory
甚至我的系统目录中也有这个文件
我想在同一个目录中用newname
重命名oldname
我很沮丧,帮帮我
您需要将传递给
subprocess.run()
的字符串中的oldname
和newname
替换为具有这些名称的变量的内容。由于Python3.6,您可以使用例如literal string interpolation,否则您的字符串将被解释为原样将对
subprocess.run()
的调用替换为subprocess.run(f"mv {oldname} {newname}", shell=True)
对于早于3.6的Python版本,请使用
.format()
:subprocess.run("mv {} {}".format(oldname, newname), shell=True)
相关问题 更多 >
编程相关推荐