更换操作系统与子流程调用外壳=Fals

2024-04-23 08:51:28 发布

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

要替换吗 操作系统与子流程调用():-

待处理代码修改:-你知道吗

os.system('if [ -e python ]; then mv -f test.txt /someURL/ ; fi') 

我试过:-你知道吗

subprocess.call('if [ -e python ]; then mv -f test.txt /someURL/ ; fi',shell=False)

subprocess.call(shlex.split('if [ -e python ]; then mv -f test.txt /someURL/ ; fi'),shell=False)

但两者都有错误案例:你知道吗

subprocess.call('if [ -e python ]; then mv -f test.txt /someURL/ ; fi',shell=False)
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Tags: pytesttxtfalseifusrshellcall
1条回答
网友
1楼 · 发布于 2024-04-23 08:51:28

我认为,最好使用python,而不是使用任何shell功能,因为这个问题肯定没有显示任何shell需求

import os
import shutil

if os.path.exists('/usr/bin/python'):
    shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")

相关问题 更多 >