os.copy-在当前目录中-一个文件到另一个文件不起作用

时间:2019-05-04 22:45:21

标签: python-3.x windows copy

所以我在目录中有这些文件:

file1.txt
demo.py

然后我运行以下代码(demo.py):

import os
os.system("copy file1.txt file2.txt")

但是我明白了:

CMD.EXE was started with the above path as the current directory.
UNC paths are not supported.  Defaulting to Windows directory.
The system cannot find the file specified.

一点。如果我这样做

copy file1.txt file2.txt

在命令提示符下-运行正常。这是怎么回事?在Windows上使用os.copy时,我需要完全引用该文件吗?

1 个答案:

答案 0 :(得分:0)

不建议使用

os.system()执行Shell命令。试试这个:

import subprocess  
cmd = "copy file1.txt file2.txt" 
execute = subprocess.call(cmd, shell=True)  
print('output', execute)