Re: [Tutor] Cmd line not correctly interpreted

2016-06-04 Thread Peter Otten
Alan Outhier wrote:

> I'm working on a Python script to call "sox" to convert ;ogg files to
> .mp3s.
> 
> In the following segment...:

To avoid garbled code as seen below please ensure that you post plain text. 
Thank you. 
> 
> *1outname=fname+".mp3"2fname=ReSpace(fname)   # substitute " "
> with
> "\ "3outname=ReSpace(outname)4cmdline="sox " + fname + " "
> +outname5print cmdline6rtncode=os.system(cmdline)7if rtncode
> <>
> 0:8print "Bad return code (" + str(rtncode) + ") from sox
> command"9sys.exit()*
> 
> ...I DO get the error trap (every time). Line 5 causes the following (for
> example) to be printed:
> 
> 
> *sox Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\
> the\ Blues Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\
> for\ the\ Blues.mp3*
> *Bad return code (512) from sox command *
> 
> I can however cop-past that output to bash and it works fine.
> 
> I get similar (but *as expected* more complicated problems if I comment
> out lines 2 & 3.
> 
> Please help! I'll send the whole program if requested.

The best approach is to use the subprocess module and let Python do the work 
for you. For example

import subprocess

fname = ... # may contain spaces
outname = fname + ".mp3"
# Pass arguments as a list, no need to use a shell:
subprocess.check_output(["sox", fname, outname])

If the sox invocation fails check_output will raise a CalledProcessError 
exception that you can examine for details, print or just let bubble up.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Cmd line not correctly interpreted

2016-06-04 Thread Steven D'Aprano
On Fri, Jun 03, 2016 at 06:01:48PM -0700, Alan Outhier wrote:
> I'm working on a Python script to call "sox" to convert ;ogg files to .mp3s.
> 
> In the following segment...:
> 
> *1outname=fname+".mp3"2fname=ReSpace(fname)   # substitute " " with
> "\ "3outname=ReSpace(outname)4cmdline="sox " + fname + " "
> +outname5print cmdline6rtncode=os.system(cmdline)7if rtncode <>
> 0:8print "Bad return code (" + str(rtncode) + ") from sox
> command"9sys.exit()*

Your code is a mess. Try sending plain text instead of "rich text". When 
you send rich text, your mail client will mangle the line breaks and put 
them where ever it sees fit.

Also, there's no need for line numbers. Especially not when there are 
only nine lines.


> ...I DO get the error trap (every time). Line 5 causes the following (for
> example) to be printed:
> 
> 
> *sox Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ the\
> Blues Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\
> the\ Blues.mp3*
> *Bad return code (512) from sox command *

According to the sox man page, it will only return 0, 1 or 2, not 512:

Exit status is 0 for no error, 1 if there is a problem with 
the command-line parameters, or 2 if an error occurs during
file processing.

http://sox.sourceforge.net/sox.html#DIAGNOSTICS

but I think that is a lie, as I can reproduce your error, without even 
using spaces:

py> os.system("sox ab cd")
sox: Can't open input file 'ab': No such file or directory
512

I'm surprised that you don't see the error message printed by sox. Are 
you redirecting stderr to a file or something? If so, please check the 
file.

You are using a relative path starting with "Carnegie\ Hall...". You 
probably should use an absolute path.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Cmd line not correctly interpreted

2016-06-04 Thread Alan Gauld via Tutor
On 04/06/16 02:01, Alan Outhier wrote:
> I'm working on a Python script to call "sox" to convert ;ogg files to .mp3s.

1outname=fname+".mp3"
2fname=ReSpace(fname)   # substitute " " with "\ "
3outname=ReSpace(outname)
4cmdline="sox " + fname + " " +outname
5print cmdline
6rtncode=os.system(cmdline)
7if rtncode <> 0:
8print "Bad return code (" + str(rtncode) + ") from sox command"
9sys.exit()*

> ...I DO get the error trap (every time). Line 5 causes the following (for
> example) to be printed:
> 
> *sox Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\ the\
> Blues Carnegie\ Hall\ Jazz\ Band/Carnegie\ Hall\ Jazz\ Band/Frame\ for\
> the\ Blues.mp3*
> *Bad return code (512) from sox command *

> I can however cop-past that output to bash and it works fine.

It might be easier to use the subprocess call() function
and pass the filenames in that way. Also you could try upping the
verbosity of sox so it tells you more about the error.

I'd be suspicious of the filename format and try using
hard coded strings in raw format first:

outname = r"Carnegie Hall Jazz Band/Carnegie Hall Jazz Band/Frame for
 the Blues.mp3"

If that works it suggests your formatting function is not doing
what it should.

Another thing to try is to eliminate the complex filenames entirely
by copying the file to foo.ogg in the local directory. See if that
works. Then try a name with spaces in the current directory. Then a
directory name without spaces. etc etc.

By a process of elimination you can find out where things start
to fall over.

> Please help! I'll send the whole program if requested.

The ReSpace() function might be helpful.

But please send using plain text. Trying to reconstruct Python
code as I did above is extremely error prone.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor