1

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.

киска
  • 137
  • 1
  • 7

1 Answers1

1

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.)

finefoot
  • 9,914
  • 7
  • 59
  • 102
  • 2
    @Kiska Try using `os.startfile(destination_file)` instead. – ekhumoro Jul 22 '19 at 21:56
  • 1
    Definitely use `os.startfile` instead of CMD. There's no point in starting cmd.exe; risking whatever way CMD parses the command line; and then having CMD try `CreateProcessW` before finally calling `ShellExecuteExW`. With `os.startfile`, we're directly calling `ShellExecuteW`. But if you do use subprocess with `shell=True`, never pass a list in this case. In almost every case it's wrong in Unix, and in Windows, where we have to build a command line string anyway, `subprocess.list2cmdline` does not implement quoting for CMD (as if anyone could... its rules are such a mess). – Eryk Sun Jul 23 '19 at 00:35