run(“mv source destation”,shell=True),输出时不存在这样的文件或目录,甚至不存在这样的文件

2024-09-28 05:28:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我的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']

只是名字janejdoe改变了 这是我的剧本

#!/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

我很沮丧,帮帮我


Tags: csvnamehomenewdatadoccontactprofile
1条回答
网友
1楼 · 发布于 2024-09-28 05:28:57

您需要将传递给subprocess.run()的字符串中的oldnamenewname替换为具有这些名称的变量的内容。由于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)

相关问题 更多 >

    热门问题