Python error for space in path - python

In python if I try to give path with space I'm getting error as below
import os
os.system("C:\Program Files (x86)\(application.exe)")
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
How can I give path with space?

You can make it working by using r.
E.g. :
import os
cmd =r'"C:\Program Files (x86)\Java\jre7\bin\java.exe"'
os.system(cmd)

Related

How to zip all outputs (*.csv and *.tiff) in jupyter notebook

I use this command
! zip -r results.zip . -i *.csv *.pdf
in Jupyter Notebook(Python 3.7.10) in order to zip all the output files. But, it shows
zip' is not recognized as an internal or external command, operable program or batch file.
Can anyone suggest what I miss?
I think you are trying to use,os.system():
If you are using linux
import os
os.system("zip -r results.zip . -i *.csv *.pdf")
#OR
import subprocess
subprocess.Popen("zip -r results.zip . -i *.csv *.pdf")
If you aren't using linux, in windows. There is library called zipfile, you can use it:
from zipfile import ZipFile
import os
filesname=os.listdir("<path or empty") # Or you can alternately use glob inside `with` ZipFile
# Empty means working folder
with ZipFile('output.zip', 'w') as myzip:
for file in files:
if file.endswith(".csv") and file.endswith(".pdf"):
myzip.write(file)
On modern Windows the Zip tool is Tar
It is not well documented nor as easy to use as most 3rd Party zippers thus you would need to custom wrap your OS call.
generally the following should be good enough
Tar -a -cf results.zip *.csv *.pdf
However if there are not one or other type the response will complete for the valid group, but with a very cryptic response:-
Tar: : Couldn't visit directory: No such file or directory
Tar: Error exit delayed from previous errors.

'tee' is not recognized as an internal or external command, operable program or batch file

I run some code in python on windows and I get an error of " 'tee' is not recognized as an internal or external command, operable program or batch file. "
result = './model/%s/result.txt' % opt.name # path of result.txt file
os.system('python evaluate_gpu.py | tee -a %s' % result)
I know tee does not exist on windows can anyone help me and write replace code for windows version
Thank all
First of all, you can download UnxUtils from http://sourceforge.net/projects/unxutils/?source=dlp. Then, you need to find tee.exe and move it to the folder of system32 in your pc.

Batch file to execute python script

I'm trying to write a batch file to execute a python script and not having much luck. I tried the following:
#echo off
SET path C:\"Program Files\python37\python.exe"
C:\"projects\systemcheck.py -c systems.csv"
but get the following error:
C:\projects>nexus-script.bat Environment variable path C:\"Program
Files\python37\python.exe" not defined 'C:\"projects\systemcheck.py -c
systems.csv"' is not recognized as an internal or external command,
operable program or batch file.
It may be easiest to specify the full path to the Python executable. That way you don't have to worry about the PATH environment variable.
#echo off
"C:\Program Files\python37\python.exe" C:\projects\systemcheck.py -c systems.csv
If you absolutely need to set the PATH environment variable, you would do it like this:
#echo off
SET "PATH=C:\Program Files\python37;%PATH%"
python C:\projects\systemcheck.py -c systems.csv
Note that the path to the Python folder goes before the previous contents of PATH; this ensures that that is the Python that gets run if you have multiple Python installations on you computer.
You need to have your FULL path in quotes. Example: SET path to "C:\Program Files\python37\python.exe"
You don't need to SET the path, you can do what Jack mentioned in a past comment.

CMD can't normaly read 'C:\Program Files'

I have some problem with my WIndows CMD.
Some time I need to open python file using CMD command. And I write: 'C:\Program Files\Python X.X\python.exe file.py' but have error: 'C:\Program' isn't system command (maybe not the same, I have another OS language).
With different methods I have different errors but can't open python file.
Examples:
(Picture) translate: can't find 'C:\Program'...
(Picture) another example when I trying to write python directory first and then start python file, but it can't find python file.
Thanks for helping me.
There seems to be 2 different problems here.
Windows does not recognise spaces in directory or file names on the command line, so you need to put the directory insied "" .
i.e. "C:\Program Files\Python 3.4\python.exe"
In your second picture, suggests that run.py does not exist in the current directory. Change Directory to where the run.py file is before running that command.
First of all go to the directory where your python file is located ... like:
cd "c:\users\someone\documents\..."
On your pictures you are trying to run python file located in system32 folder but i guess it is not located there so move where the file is with that cd command
Then as Martin says the problem with path of python.exe is the space between words. To solve put the path into quotation marks.
But u can add python to system path and insted of writing full path u can write only
python file.py
How to add python to path see here https://superuser.com/questions/143119/how-to-add-python-to-the-windows-path

System Error on running a powershell command from a python script

I am trying to run following python script on windows powershell which is throwing following error
ERROR:
The term 'x86' is not recognized as the name of a cmdlet, function, script file
, or operable program. Check the spelling of the name, or if a path was include
d, verify that the path is correct and try again.
SCRIPT:
import os
import sys
Watchdog_config = 'C:\\Program Files (x86)\\Common Files\\ibm\\icc\\cimom\\data\\wmia.properties'
command1 = "PowerShell -Command \"& {(cat "+Watchdog_config+" )|%{$_ -replace {\"off\",\"on\"}}|set "+Watchdog_config+"}\""
os.system(command1)
Can you try :
Watchdog_config = '\"C:\\Program Files (x86)\\Common Files\\ibm\\icc\\cimom\\data\\wmia.properties\"'
I'am not confortable with python, I just want to enclose path into double quotes "".

Categories