[英]subprocess.call() in windows 10 returns an error that it can't find the file
我正在开发一个需要一些修改才能在 Windows 10 下工作的 unix 脚本。该脚本使用子进程使用 DOS 命令执行文件操作。 具体来说,使用 subprocess 将文件从目录复制到当前目录的语句会返回错误消息。 正确的 DOS 命令是
copy "D:\tess\catalog files\TIC55234031.fts" .
然而,
ref_fits_filename="D:\TESS\catalog files\TIC55234031.fts"
和
subprocess.call(['copy ', ref_fits_filename,' .']) # copy the ref fits file to here
旨在执行完全相同的操作,出错了:
Traceback (most recent call last): File "EBcheck.py", line 390, in subprocess.call(['copy ', ref_fits_filename,' .']) # copy the ref fits file to here File "C:\Users\FD-Computers\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 267, in call with Popen(*popenargs, **kwargs) as p: File "C:\Users\FD-Computers\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "C:\Users\FD-Computers\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 997, in _execute_child startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified
显然,一定有一个微妙的语法错误或一个我没有想到的问题会导致问题。 是否有 Windows 10 下的 Python 编程专家来澄清此论坛上的问题? 这里使用的 Python 版本是最近的 Python 3.6.4。
造成这种情况的原因很多:
copy
是内置的 Windows shell,它不是可执行文件。 你必须使用shell=True
subprocess.call(['copy ', ref_fits_filename,' .'])
在参数中有空格所以你可以这样做:
subprocess.call(['copy', ref_fits_filename,'.'],shell=True)
但最pythonic的方法是放弃所有这些并使用shutil.copy
import shutil
shutil.copy(ref_fits_filename,".")
作为奖励,如果副本出错,您会得到一个干净的 python 异常。
旁白:在将 Windows 路径定义为文字时始终使用原始前缀:
ref_fits_filename = r"D:\tess\catalog files\TIC55234031.fts"
(我猜您将tess
大写为TESS
以解决\\t
是制表字符的事实:))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.