繁体   English   中英

Windows 10 中的 subprocess.call() 返回找不到文件的错误

[英]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。

造成这种情况的原因很多:

  1. copy是内置的 Windows shell,它不是可执行文件。 你必须使用shell=True
  2. 您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM