This question already has answers here:
Why can't Python's raw string literals end with a single backslash?
(13 answers)
Closed last year.
I need help with this problem:
temp = "video.mp4"
way_to_file = r"C:\Users\Lukas\Desktop\auto youtube channel\" + temp
the problem is I close the string and can put it together the string and temp I mean It is not working
error:
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
subprocess.call(['hello.py', 'htmlfilename.htm'])
File "C:\Python34\lib\subprocess.py", line 537, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python34\lib\subprocess.py", line 858, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1111, in _execute_child
startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application
You can use just
way_to_file = f"C:\\Users\\Lukas\\Desktop\\auto youtube channel\\{temp}"
or
way_to_file = rf"C:\Users\Lukas\Desktop\auto youtube channel\{temp}"
try instead:
temp = "video.mp4"
way_to_file = "C:\\Users\\Lukas\\Desktop\\auto youtube channel\\" + temp
Related
This question already has answers here:
What exactly is current working directory?
(5 answers)
Closed 27 days ago.
I am using a python package from Gray Arrow's Dejavu. I have installed all the required dependencies and other packages necessary to run my code. When running the following code:
import warnings
import json
warnings.filterwarnings("ignore")
from dejavu import Dejavu
from dejavu.recognize import FileRecognizer
with open('dejavu.cnf') as f:
config = json.load(f)
djv = Dejavu(config)
djv.fingerprint_file('mp3/Sean-Fournier--Falling-For-You.mp3')
song = djv.recognize(FileRecognizer, 'mp3/Sean-Fournier--Falling-For-
You.mp3')
print("from file recognized: {}\n".format(song))
I receive the following errors:
Traceback (most recent call last):
File "testing.py", line 13, in <module>
djv.fingerprint_file('mp3/Sean-Fournier--Falling-For-You.mp3')
File "D:\dejavu-master\dejavu\__init__.py", line 109, in
fingerprint_file song_name=song_name
File "D:\dejavu-master\dejavu\__init__.py", line 182, in
_fingerprint_worker channels, Fs, file_hash = decoder.read(filename,
limit)
File "D:\dejavu-master\dejavu\decoder.py", line 51, in read
audiofile = AudioSegment.from_file(filename)
File "C:\Program Files\Python37\lib\site-
packages\pydub\audio_segment.py", line 665, in from_file info =
mediainfo_json(orig_file)
File "C:\Program Files\Python37\lib\site-packages\pydub\utils.py",
line 263, in mediainfo_json res = Popen(command,
stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
File "C:\Program Files\Python37\lib\subprocess.py", line 775, in
__init__ restore_signals, start_new_session)
File "C:\Program Files\Python37\lib\subprocess.py", line 1178, in
_execute_child startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file
specified
I have done complete re-installations of python 3.7 and all of the packages and dependencies to attempt correction of this problem.
I do not understand what file cannot be found and have done research in hopes to find an answer to this issue. I truly appreciate any help and knowledge that can be shared with me on this topic.
Try and place "Sean-Fournier--Falling-For-You.mp3" file in the exact directory you have your script, I don't think the folder reference to the mp3 folder is being handled correctly :)
djv.fingerprint_file('mp3/Sean-Fournier--Falling-For-You.mp3')
song = djv.recognize(FileRecognizer, 'mp3/Sean-Fournier--Falling-For-
You.mp3')
This question already has an answer here:
subprocess gives an error. "The system cannot find the file specified"
(1 answer)
Closed 4 years ago.
I have subrpocess function to start a postgres table. But I'm getting a file path error
filepath = self.get_path(filename)
...
if not append:
# Create the new table itself without adding actual data.
print "this is the path {}\n\n".format(filepath)
print "does file exist".format(os.path.isfile(filepath))
create_table = subprocess.Popen(['shp2pgsql', '-p', '-I',
'-s', str(srid),
'-W', encoding,
filepath, table],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
this is the traceback I'm getting:
this is the path C:/Urbansims/inputs\geographies/cities.shp
does file exist? True
Traceback (most recent call last):
File "loading_data.py", line 117, in <module>
loader.load_shp_map({value["sql_in"]:"/".join(value["input"].rsplit("/",2)[1:]) for key,value in geo_inputs.iteritems()})
File "c:\urbansims\libs\spandex\spandex\io.py", line 416, in load_shp_map
self.load_shp(filename=value, table=table, drop=True)
File "c:\urbansims\libs\spandex\spandex\io.py", line 369, in load_shp
universal_newlines=True)
File "C:\Anaconda2\lib\subprocess.py", line 394, in __init__
errread, errwrite)
File "C:\Anaconda2\lib\subprocess.py", line 644, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
the path exist and the os.find.path is returning a True for the location. Why cant the system find the path if it clearly is the correct path to the file?
You are using filepath as an argument to the shp2pgsql tool that you are trying to launch in a separate process. The error comes from shp2pgsql not being found.
I've search a while and still can not figure it out...
Here's part of my code that went wrong.
import subprocess as sp
import os
cmd_args = []
cmd_args.append('start ')
cmd_args.append('/wait ')
cmd_args.append(os.path.join(dirpath,filename))
print(cmd_args)
child = sp.Popen(cmd_args)
And the command prompt through out this.
['start ', '/wait ', 'C:\\Users\\xxx\\Desktop\\directory\\myexecutable.EXE']
Traceback (most recent call last):
File "InstallALL.py", line 89, in <module>
child = sp.Popen(cmd_args)
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child startupinfo)
FileNotFoundError: [WinError 2]
It looks like the filepath is wrong with 2 backslashes.
I know if I do
print(os.path.join(dirpath,filename))
It'll return
C:\Users\xxx\Desktop\directory\myexecutable.EXE
I'm sure the file is there.
How can I debug this?
This is happening because Popen is trying to find the file start instead of the file you want to run.
For example, using notepad.exe:
>>> import subprocess
>>> subprocess.Popen(['C:\\Windows\\System32\\notepad.exe', '/A', 'randomfile.txt']) # '/A' is a command line option
<subprocess.Popen object at 0x03970810>
This works fine. But if I put the path at the end of the list:
>>> subprocess.Popen(['/A', 'randomfile.txt', 'C:\\Windows\\System32\\notepad.exe'])
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
subprocess.Popen(['/A', 'randomfile.txt', 'C:\\Windows\\System32\\notepad.exe'])
File "C:\python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Use this instead:
import subprocess as sp
import os
cmd_args = []
cmd_args.append(os.path.join(dirpath,filename))
cmd_args.append('start ')
cmd_args.append('/wait ')
print(cmd_args)
child = sp.Popen(cmd_args)
You might need to swap cmd_args.append('start ') and cmd_args.append('/wait ') around too depending on which order they are meant to be in.
I faced the same problem and just to add a note about Popen: As argument Popen takes a list of strings for non-shell calls and only a string for shell calls.
Details listed here: WinError 2 The system cannot find the file specified (Python)
This question already has answers here:
How can I access environment variables in Python?
(16 answers)
Closed 6 years ago.
Related to this question
The question in principle is the same, I have a subprocess.system call
...
EDITOR = os.environ.get('EDITOR', 'vim')
subprocess.call([EDITOR, tf.name])
...
Where EDITOR is the environments $EDITOR variable, tf.name is just a filename.
However, sublime text suggest to set the $EDITOR to export EDITOR='subl -w' Making my call look like this:
subprocess.call(['subl -w', "somefilename"])
And it fails like this:
raceback (most recent call last):
File "/usr/bin/note", line 65, in <module>
storage["notes"][args.name] = writeNote(args.name, storage)
File "/usr/bin/note", line 54, in writeNote
subprocess.call([EDITOR, tf.name])
File "/usr/lib/python3.5/subprocess.py", line 557, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1541, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'subl -w'
Of course, it's supposed to look likie this
subprocess.call([subl", "-w" "somefilename"])
A workaround would perhaps be
args = EDITOR.split(" ")
subprocess.call(args + ["somefilename"])
But I'm a little wary of doing this, because I cannot know what $EDITOR is set to, is this safe to do?
What's the proper way to handle this case?
You can rather use shlex. It takes care of UNIX shell like commands.
For example:
>>> shlex.split( "folder\ editor" ) + ["somefilename"]
['folder editor', 'somefilename']
>>> shlex.split( "editor -arg" ) + ["somefilename"]
['editor', '-arg', 'somefilename']
So you should be able to directly do:
subprocess.call( shlex.split(EDITOR) + ["somefilename"] )
This question already has answers here:
Python: How to get stdout after running os.system? [duplicate]
(6 answers)
Closed 3 years ago.
I tried this:
import os
if os.system('somecommand') == 'output from command':
do_something()
But it didn't worked. How can I do it? What library should I use?
When I use solution with subprocess below I get this errorcode:
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
The return value of os.system() is the exit code of the process, not whatever it outputs to stdin. You want to use the subprocess module instead:
import subprocess
if subprocess.check_output('somecommand') == 'output from command':