I am having trouble opening an image with the neural_style script found here. When passing arguments to the script in Spyder's IPython console, I have continually received the following error:
OSError: [Errno 22] Invalid argument:'<C:/Users/Caleb/Documents/Python_Scripts/pytorch/project1/content_images/20180727_201120.jpg>'
I have attempted changing forward slashes to backslashes, using a filepath that starts at my working directory (not including the folders above it), and eliminating the filepath from the entry (i.e. simply entering the filename). None of these fixes have solved my problem. The arguments follow the form:
python neural_style/neural_style.py eval \
--content-image </path/to/content/image> \
--model </path/to/saved/model> \
--output-image </path/to/output/image> \
--cuda 1
What amendments must I make to my arguments to no longer receive this error? I am using Windows 10 and Python 3.6.5. The arguments are read by argparse.
Please note that I used () instead of <> above, not sure how to properly use <> without deleting the interior content.
Thank you!
Related
I am doing a script using SoX to merge multiple audio file together.
This command works in the terminal
sox &(ls *.mp3) out.mp3
but if I try using it inside a python script by calling subprocess.run() it doesn't
subprocess.run(['sox', '$(ls *.mp3)', 'out.mp3'])
> sox FAIL formats: can't open input file `$(ls *.mp3)': No such file or
> directory
I image that is because of the subshell operation, but I don't know how to pass it correctly.
I also tried, as some other post suggested, passing the argument shell=True but then it says
> sox FAIL sox: Not enough input filenames specified
I am in the same working directory and I also tried supplying the full path but doesn't work either.
I could just write a bash script and call it, but I would like to know how to deal in this scenario with Python.
you want to use shell=True to force subprocess to run your command through the shell interpreter and parse the wildcards/sub-commands. However this (depending on the platform) imposes that the argument is passed as string, not as list of parameters. A lot of constraints for a lazy & unsafe way of doing it.
Wait. You can do without shell=True using glob.glob:
subprocess.run(['sox'] + glob.glob('*.mp3') + ['out.mp3'])
Would be better to check if there actually are mp3 files in the current folder so:
input_files = glob.glob('*.mp3')
if input_files:
subprocess.run(['sox'] + input_files + ['out.mp3'])
else:
raise Exception("No mp3 files")
if you get the "No mp3 files" message, then check the current directory. It's always good to use a parameter for the input directory, and avoid relying on the current directory (glob.glob(os.path.join(input_directory,'*.mp3')))
First of all, I am new to programming.
To run python code in an external shell window, I followed the instructions given on this page
link
My problem is that if I save the python file in any path that contains a folder name with a space, it gives me this error:
C:\Python34\python.exe: can't open file 'C:\Program': [Errno 2] No such file or directory
Does not work:
C:\Program Files\Python Code
Works:
C:\ProgramFiles\PythonCode
could someone help me fix the problem???
Here is the code:
import sublime
import sublime_plugin
import subprocess
class PythonRunCommand(sublime_plugin.WindowCommand):
def run(self):
command = 'cmd /k "C:\Python34\python.exe" %s' % sublime.active_window().active_view().file_name()
subprocess.Popen(command)
subprocess methods accept a string or a list. Passing as a string is the lazy way: just copy/paste your command line and it works. That is for hardcoded commands, but things get complicated when you introduce parameters known at run-time only, which may contain spaces, etc...
Passing a list is better because you don't need to compose your command and escape spaces by yourself. Pass the parameters as a list so it's done automatically and better that you could do:
command = ['cmd','/k',r"C:\Python34\python.exe",sublime.active_window().active_view().file_name()]
And always use raw strings (r prefix) when passing literal windows paths or you may have some surprises with escape sequences meaning something (linefeed, tab, unicode...)
In this particular case, if file associations are properly set, you only need to pass the python script without any other command prefix:
command = [sublime.active_window().active_view().file_name()]
(you'll need shell=True added to the subprocess command but it's worth it because it avoids to hardcode python path, and makes your plugin portable)
I'm attempting to use the python 010 editor template parser
The doc specifically states (to get started):
import pfp
pfp.parse(data_file="C:\path2File\file.SWF",template_file="C:\path2File\SWFTemplate.bt")
However, it throws:
RuntimeError: Unable to invoke 'cpp'. Make sure its path was passed correctly
Original error: [Error 2] The system cannot find the file specified
I've tried everything, from using raw strings:
df = r"C:\path2File\file.swf"
tf = r"C:\path2File\SWFTemplate.bt"
To single and then double '\'s or '/'s in the string. However, it keeps throwing the above error message.
I checked the files are in the path and ensured everything is properly spelled, case sensitively.
To test my paths, I've used the windows "type" (equiv to *nix strings) and passed the strings as args in a subprocess.Popen which worked.
The problem is that it's trying to invoke a C++ compiler: cpp and you don't have one.
You'll need to install one, or make sure that your PATH has a cpp.exe on it somewhere.
I wanted to play a .wav file, without using external modules, and i read i could do that using this:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", /Users/me/Downloads/sample.wav])
I however get:
SyntaxError: invalid syntax
If i use os.path.realpath to get the absolute path of the file, i get just the same thing. (The path i see at get info)
Environment is OSX, Python 2.7
Can someone tell me what i am doing wrong? I am new to Python (and to Programming).
There are multiple problems.
Indentation
Code inside the function should be indented, to show that it is part of the function
File name should be in a quotes
It should be a string
It should be:
def play(audio_file_path):
subprocess.call(["ffplay", "-nodisp", "-autoexit", "/Users/me/Downloads/sample.wav"])
I'm trying to record video (with audio!) in this way:
ffmpeg = "C:\bin\ffmpeg.exe"
cmd = '%s -r 15 -f vfwcap -i 0 c:/output2.mpg' % (ffmpeg)
os.system(cmd)
And I have the error: "The filename, directory name, or volume label syntax is incorrect." I think that this is a problem with vfwcap, but I don't know how to fix it.
Any ideas? Maby something else is wrong?
I think mermoz must be joking with you. You've got a few problems here. Python uses '\' as an escape character, so it won't find your file unless you either double them up or switch to forward slashes, as you've then done in your cmd. The syntax of your ffmpeg command line is also completely wrong. You're saying you want to set the frames per minute to 15 and format vfwcap to your inputfile, which is "0." Also you shouldn't use os.system. Use subprocess.popen and pass your commands as lists. Not sure if this question is serious, but if so, this should start you in the right direction.
isn't it just the small c in "c:/output.mpg" instead of "C:/output.mpg"?
The direct problem is that the \ in the command line are being interpreted as control chars, use either c: \ \ or use c:/
As Profane says you have the output file flags wrong for ffmpeg