Python中的7z subprocess.Popen FileNotFoundError

2024-04-26 05:14:54 发布

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

在我的Windows命令提示符下,我正在使用7z x test.zip -r -y -o*成功解压缩我的文件

我在python中尝试以下方法:

def extractfiles(zipname):
    system = subprocess.Popen(['7z', 'x', zipname,'-r','-y','-o*'])
    return(system.communicate())
os.chdir('directory_with_my_test_file')
extractfiles('test.zip')

但是收到这个错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-99-7a976fc4b8be> in <module>
      1 import subprocess
      2 cmd = ['7z', 'x', zipname,'-r','-y','-o*']
----> 3 sp = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
      4 

~\AppData\Local\Continuum\anaconda3\envs\pyTorch_fast07\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    727                                 c2pread, c2pwrite,
    728                                 errread, errwrite,
--> 729                                 restore_signals, start_new_session)
    730         except:
    731             # Cleanup if the child failed starting.

~\AppData\Local\Continuum\anaconda3\envs\pyTorch_fast07\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1015                                          env,

       1016                                          os.fspath(cwd) if cwd is not None else None,
    -> 1017                                          startupinfo)
       1018             finally:
       1019                 # Child is launched. Close the parent's copy of those pipe

    FileNotFoundError: [WinError 2] The system cannot find the file specified

我做错了什么


Tags: theintestenvnewsessionrestoresystem