киска
киска

Reputation: 137

Subprocess.run() cannot find file, even if path.exists() returns true

I found these two pages:

Subprocess.run() cannot find path

Python3 Subprocess.run cannot find relative referenced file

but it didn't help. The first page talks about using \\ but I already do, and the second one talks about double quotes around one of the arguments.

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)

The program gets the company name and position from the user, generates the current date, creates the directories, and then moves/renames the base resume based on the user input.

Output and Results:

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

Error Message:

[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

The if statement returns True but subprocess.run() cannot see the file, but I'm not really sure why.

Upvotes: 1

Views: 4257

Answers (1)

finefoot
finefoot

Reputation: 11224

On which operating system are you? The backslashes in your path suggest that you're on Windows and you're using open to open the document with its default application. However, looking at this question Open document with default OS application in Python, both in Windows and Mac OS you should use start instead of open for Windows:

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

Also you need to add shell=True for start to work. However, you should read https://docs.python.org/3/library/subprocess.html#security-considerations beforehand.

(I suspect, the error [WinError 2] The system cannot find the file specified appears, because Windows cannot find open - it's not about the document you're trying to open.)

Upvotes: 1

Related Questions