即使path.exists()返回true,Subprocess.run()也找不到文件

时间:2019-07-22 21:35:21

标签: python python-3.x windows subprocess

我找到了这两页:

Subprocess.run() cannot find path

Python3 Subprocess.run cannot find relative referenced file

但是没有帮助。第一页讨论使用\\,但我已经做过,第二页讨论围绕一个参数的双引号。

work = Path("R:\\Work")
resume = work.joinpath("cover_letter_resume_base.doc")

current_date = construct_current_date()

company_name = gather_user_information(question="Company name: ", 
                                        error_message="Company name cannot be empty")

position = gather_user_information(question="Position: ", 
                                   error_message="Position cannot be empty")

# Construct destination folder string using the company name, job title, and current date
destination = work.joinpath(company_name).joinpath(position).joinpath(current_date)

# Create the destintion folder
os.makedirs(destination, exist_ok=True)

# Construct file name
company_name_position = "{0}_{1}{2}".format(company_name.strip().lower().replace(" ", "_"), 
                                position.strip().lower().replace(" ", "_"), resume.suffix)

resume_with_company_name_job_title = resume.stem.replace("base", company_name_position)
destination_file = destination.joinpath(resume_with_company_name_job_title)

# Copy and rename the resume based on the company and title.
shutil.copy2(src=resume, dst=destination_file)

if destination_file.exists():
    print(f"{destination_file} created.")
    #subprocess.run(["open", str(destination_file)], check=True)

程序从用户那里获取公司名称和职位,生成当前日期,创建目录,然后根据用户输入移动/重命名基本简历。

输出和结果

Company name: Microsoft
Position: Software Eng
R:\Work\Microsoft\Software Engineer\20190722\cover_letter_resume_microsoft_software_eng.doc 
created.

错误消息

[WinError 2] The system cannot find the file specified
Traceback (most recent call last):
  File "c:/Users/Kiska/python/job-application/main.py", line 59, in <module>
    main()
  File "c:/Users/Kiska/python/job-application/main.py", line 53, in main
    raise error
  File "c:/Users/Kiska/python/job-application/main.py", line 48, in main
    subprocess.run(["start", str(destination_file)], check=True)
  File "C:\Program Files (x86)\Python37-32\lib\subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Program Files (x86)\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files (x86)\Python37-32\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

if语句返回True,但是subprocess.run()无法看到文件,但我不确定为什么。

1 个答案:

答案 0 :(得分:1)

您在哪个操作系统上?路径中的反斜杠表示您使用的是Windows,并且使用open来打开文档及其默认应用程序。但是,对于这个问题Open document with default OS application in Python, both in Windows and Mac OS,对于Windows,您应该使用start而不是open

subprocess.run(["start", str(destination_file)], check=True, shell=True)

此外,您需要添加shell=True才能使start正常工作。但是,您应该事先阅读https://docs.python.org/3/library/subprocess.html#security-considerations

(我怀疑出现错误[WinError 2] The system cannot find the file specified,因为Windows找不到open-与您要打开的文档无关。)