Python Forum
Subprocess.Popen() not working when reading file path from csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subprocess.Popen() not working when reading file path from csv file
#1
I am a beginner at python coding.

I wrote a simple code that lists a number of programs and asks the user to choose one of the programs. It will then open a website with the version history of that program, and, where it exists, run the updater of the program.

Initially I wrote the program manually writing out the list of programs (20) and then manually write out the url and, where relevant, the path and file name to the updater. Something like:

if choice == 1
   url = 'https://blablabla'
   prg = 'C:\\Program Files\\blablabla'
After manually defining the URL and prg variables for all 20 options, I run:

webbrowser.open_new_tab(url)
subprocess.Popen(prg)
This works fine.

Then I tried to write the code where the list of programs and the url and file paths are listed in a .csv file. The values for the url and prg variables are read from the .csv file (using numpy load.txt)

webbrowser.open_new_tab(url)
works fine

However:

subprocess.Popen(prg)
gives an error message if the file path has a blank space in it (which is in 19 out of the 20 options). The error message is:

Error:
Traceback (most recent call last): File "test.py", line 32, in <module> mainmenu() File "test.py", line 30, in mainmenu subprocess.Popen(prg) File "C:\Users\zorin\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 858, in _init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\zorin\AppData\Local\Programs\Python\Python38\lib\subprocess.py", line 1311, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified
One example:

In the original (long-winded)code I manually set the full file name for Chrome:
prg = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
and

subprocess.Popen(prg)
runs Chrome without a problem.

In the updated code the same full file name is read into prg, but this time

subprocess.Popen(prg)
gives the above-mentioned error code (if the file path has a black space it).

In the new code
print(prg)
will give:
Output:
C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe
so the correct value is read into the variable.

Any suggestions what I am doing wrong?

I use Windows
Reply
#2
Try subprocess.Popen([prg]) perhaps.
Reply
#3
(May-03-2021, 07:51 PM)herwin Wrote: In the new code

print(prg)
will give:
Output:
C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe
so the correct value is read into the variable.

No it is not correct. The path should be: "C:\Program Files\Google\Chrome\Application\chrome.exe", so without the double backlashes.
The backslash has a special meaning in Python. So to assign a string literal with backslashes you have to double the backslash to make clear you really mean one backslash.
>>> prg = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
>>> print(prg)
C:\Program Files\Google\Chrome\Application\chrome.exe
So "prg" will contain only single backslashes.
Nowadays a more popular way to assign such a variable is to assign a raw string by putting an "r" before the string literal.
>>> prg = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
>>> print(prg)
C:\Program Files\Google\Chrome\Application\chrome.exe
So make sure you get rid of the double backslashes.
Reply
#4
(May-03-2021, 07:51 PM)herwin Wrote: gives the above-mentioned error code (if the file path has a black space it).

In the new code
print(prg)
Do print(repr(prg)) then you see if there is a space.
It's normal when read from file eg csv to use strip() so there no space at beginning or end.
>>> path = r'C:\code\file.csv '
# Looks ok,but are not
>>> print(path)
C:\code\file.csv 
>>> 
>>> # To see all
>>> print(repr(path))
'C:\\code\\file.csv '
>>> 
>>> # Fix
>>> path.strip()
'C:\\code\\file.csv'
Reply
#5
(May-03-2021, 09:15 PM)Gribouillis Wrote: Try subprocess.Popen([prg]) perhaps.

Thank you for your answer.

Unfortunately the problem remains the same
Reply
#6
(May-04-2021, 08:28 AM)ibreeden Wrote:
(May-03-2021, 07:51 PM)herwin Wrote: In the new code

print(prg)
will give:
Output:
C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe
so the correct value is read into the variable.

No it is not correct. The path should be: "C:\Program Files\Google\Chrome\Application\chrome.exe", so without the double backlashes.
The backslash has a special meaning in Python. So to assign a string literal with backslashes you have to double the backslash to make clear you really mean one backslash.
>>> prg = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
>>> print(prg)
C:\Program Files\Google\Chrome\Application\chrome.exe
So "prg" will contain only single backslashes.
Nowadays a more popular way to assign such a variable is to assign a raw string by putting an "r" before the string literal.
>>> prg = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
>>> print(prg)
C:\Program Files\Google\Chrome\Application\chrome.exe
So make sure you get rid of the double backslashes.

Thank you for your response and suggestions.

I have tried with the single and double backslashes in the csv file. The error message stays the same in both cases.

How do I read the values from the csv file into the prg variable as a raw string?
Reply
#7
(May-04-2021, 09:54 AM)snippsat Wrote:
(May-03-2021, 07:51 PM)herwin Wrote: gives the above-mentioned error code (if the file path has a blank space it).

In the new code
print(prg)
Do print(repr(prg)) then you see if there is a space.
It's normal when read from file eg csv to use strip() so there no space at beginning or end.
>>> path = r'C:\code\file.csv '
# Looks ok,but are not
>>> print(path)
C:\code\file.csv 
>>> 
>>> # To see all
>>> print(repr(path))
'C:\\code\\file.csv '
>>> 
>>> # Fix
>>> path.strip()
'C:\\code\\file.csv'

Thanks.

I check as per your suggestion and there are no spaces at the beginning or the end of the string.
The problem appears to be the blank space in the middle of the string (between 'program' and 'files' in the file path)
Reply
#8
Have you tried Popen([prg]) instead of Popen(prg) ? The less ambiguous way to call Popen() is to pass a list of the arguments.
Reply
#9
(May-04-2021, 03:42 PM)Gribouillis Wrote: Have you tried Popen([prg]) instead of Popen(prg) ? The less ambiguous way to call Popen() is to pass a list of the arguments.

Thanks.
Yes, I did. Unfortunately that does not seem to solve the problem (I get the same error message)
Reply
#10
(May-04-2021, 03:30 PM)herwin Wrote: The problem appears to be the blank space in the middle of the string (between 'program' and 'files' in the file path)
Should not be a problem,a quick test.
So this run fine no error,and open programs.
import subprocess

core = r'C:\Program Files\Core Temp\Core Temp.exe'
prg = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
print(repr(core))
print(repr(prg))
subprocess.Popen([core])
subprocess.Popen([prg])
Output:
E:\div_code\new λ python p_open.py 'C:\\Program Files\\Core Temp\\Core Temp.exe' 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
If put paths is a file,run fine.
Output:
# prog.csv C:\Program Files\Core Temp\Core Temp.exe,C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
import subprocess
import csv

with open('prog.csv') as f:
    reader = csv.reader(f, delimiter=',')
    prog_lst = list(reader)

for prog in prog_lst[0]:
    subprocess.Popen([prog])
so prog_lst looks like this,se that there are two \\ when read from a file,
then there is no need for r(raw string) as if write/copy path yourself.
>>> prog_lst
[['C:\\Program Files\\Core Temp\\Core Temp.exe',
  'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe']]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use subprocess.Popen and time.sleep chucky831 2 98 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  filter every 24 days file (considering file name) RolanRoll 5 804 Jun-23-2022, 11:41 AM
Last Post: RolanRoll
  Reading .csv file doug2019 4 495 Apr-29-2022, 09:55 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 459 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Reading an Input File DaveG 1 419 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  How to split file by same values from column from imported CSV file? Paqqno 5 741 Mar-24-2022, 05:25 PM
Last Post: Paqqno
  Initializing, reading and updating a large JSON file medatib531 0 560 Mar-10-2022, 07:58 PM
Last Post: medatib531
  Converted Pipe Delimited text file to CSV file atomxkai 4 1,798 Feb-11-2022, 12:38 AM
Last Post: atomxkai
Question apk file not working on android polya001 0 509 Feb-06-2022, 11:58 PM
Last Post: polya001
  Reading Excel file and use a wildcard in file name and sheet name randolphoralph 6 1,471 Jan-13-2022, 10:20 PM
Last Post: randolphoralph

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020