By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,589 Members | 1,087 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,589 IT Pros & Developers. It's quick & easy.

Raw Strings (I Think)

P: n/a
I've used glob.glob to get a list of files in a directory
and now I want to use os.system to execute one of
those files, the problem is that python automatically
puts a escape charater infront of the back slashes
so the os.system gets X:\\####\\####\\ and is useless,
I think I need to convert my string to a raw string but
I don't know how.

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
Jul 23 '08 #1
Share this Question
Share on Google+
8 Replies

P: n/a
On Jul 24, 10:02*am, "Lanny" <la...@freshells.chwrote:
I've used glob.glob to get a list of files in a directory
and now I want to use os.system to execute one of
those files, the problem is that python automatically
puts a escape charater infront of the back slashes
so the os.system gets X:\\####\\####\\ and is useless,
I think I need to convert my string to a raw string but
I don't know how.

-- Posted on news://freenews.netfront.net - Complaints to n...@netfront.net --
This works fine for me on Windows XP.

I did this:

glob.glob(r'c:\test')

which gave me something like this:

['c:\\test\\07-24TimeSheet.xls', 'c:\\test\\accts.CSV', 'c:\\test\
\Ataris Aqu\xe1ticos #2.txt', 'c:\\test\\changes.txt', 'c:\\test\
\change_g.txt', 'c:\\test\\config.ini', 'c:\\test\\County.txt', 'c:\
\test\\county1.txt', 'c:\\test\\ctypes-1.0.1.tar.gz', 'c:\\test\
\DAMNATUS_Soundtrack.zip', 'c:\\test\\doodad.1.12.3.doc', 'c:\\test\
\doodad.1.32.3.doc', 'c:\\test\\doodad.22.12.3.doc', 'c:\\test\
\emailMess.xml', 'c:\\test\\Eula.txt', 'c:\\test\\fasta.txt', 'c:\\test
\\Funds.txt', 'c:\\test\\Funds2.txt', 'c:\\test\\Gmane.newsrc', 'c:\
\test\\groups.ini', 'c:\\test\\hammy.doc']

Now, if I use os.system like this, it works:

os.system('notepad %s' % x[1])

This opens notepad with my *.csv file just fine. Windows XP, Python
2.5.2.

Mike
Jul 23 '08 #2

P: n/a
Lanny wrote:
I've used glob.glob to get a list of files in a directory
and now I want to use os.system to execute one of
those files, the problem is that python automatically
puts a escape charater infront of the back slashes
No, it doesn't. Instead of guessing what the cause might be, please show
us your code and show us the error message you're getting, so that we
can determine what the cause really is.

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 23 '08 #3

P: n/a
Lanny wrote:
I've used glob.glob to get a list of files in a directory
and now I want to use os.system to execute one of
those files, the problem is that python automatically
puts a escape charater infront of the back slashes
so the os.system gets X:\\####\\####\\ and is useless,
No, it doesn't. The backslash doubling only happens when you "echo" a
variable to the terminal in interactive mode. If you want to know that
the string really contains, use "print".

Here's an example:
>>import glob
files = glob.glob("\\bin\\ls.exe")
files
['\\bin\\ls.exe']
>>files[0]
'\\bin\\ls.exe'
>>print files[0]
\bin\ls.exe
>>import os
os.system(files[0])
Demo Makefile.pre.in Parser
Doc Misc Python
Grammar Modules README
Include Objects RISCOS
LICENSE PC Tools
Lib PCbuild configure
Mac PCbuild8 configure.in

Btw, if you want to pass arguments to the program, you might want to use
the "subprocess" module instead, since it handles escaping and quoting
for you all by itself:
>>import subprocess
subprocess.call([files[0], "-l"])
total 479
drwxr-xr-x 26 1006 everyone 0 Oct 14 2006 Demo
drwxr-xr-x 33 1006 everyone 0 Oct 14 2006 Doc
drwxr-xr-x 6 1006 everyone 0 Oct 14 2006 Grammar
....

There's also a function called "os.startfile", which can be used to
"open" an arbitrary file (in the same as if you'd double-click on it in
the explorer).
I think I need to convert my string to a raw string but
I don't know how.
Raw strings are an alternate syntax for adding string literals to your
source code, and has nothing to do with output.

</F>

Jul 23 '08 #4

P: n/a
No, it doesn't. Instead of guessing what the cause might be, please show
us your code and show us the error message you're getting, so that we can
determine what the cause really is.
Ok, sorry. Heres my code:

import glob
import random
import os

songs = glob.glob('C:\###\###\###\*.mp3')
pick = random.choice(songs)
os.system(pick)

And yes, I know there are better ways of randomly selecting
a .mp3 file to play but I don't care.

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
Jul 23 '08 #5

P: n/a
Lanny wrote:
>No, it doesn't. Instead of guessing what the cause might be, please show
us your code and show us the error message you're getting, so that we can
determine what the cause really is.

Ok, sorry. Heres my code:

import glob
import random
import os

songs = glob.glob('C:\###\###\###\*.mp3')
pick = random.choice(songs)
os.system(pick)

And yes, I know there are better ways of randomly selecting
a .mp3 file to play but I don't care.
my guess is that the real problem is that you get back filenames with
spaces in them, which gets treated as multiple arguments by os.system.

using os.startfile will fix this:
>>import glob, os, random
file = random.choice(glob.glob("\\music\\*.mp3"))
file
'\\music\\Madrugada - Grit - 05 - Seven Seconds.mp3'
>>print file
\music\Madrugada - Grit - 05 - Seven Seconds.mp3
>>os.system(file)
'\music\Madrugada' is not recognized as an internal or external command,
operable program or batch file.
1
>>os.startfile(file)
.... music starts playing ...

</F>

Jul 23 '08 #6

P: n/a
Lanny wrote:
>No, it doesn't. Instead of guessing what the cause might be, please show
us your code and show us the error message you're getting, so that we can
determine what the cause really is.

Ok, sorry. Heres my code:
[...]
And the error message you're getting is...?

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 23 '08 #7

P: n/a
my guess is that the real problem is that you get back filenames with
spaces in them, which gets treated as multiple arguments by os.system.

using os.startfile will fix this:
>import glob, os, random
file = random.choice(glob.glob("\\music\\*.mp3"))
file
'\\music\\Madrugada - Grit - 05 - Seven Seconds.mp3'
>print file
\music\Madrugada - Grit - 05 - Seven Seconds.mp3
>os.system(file)
'\music\Madrugada' is not recognized as an internal or external command,
operable program or batch file.
1
>os.startfile(file)
... music starts playing ...

</F>
Thanks I just switched the startfile for system and it worked like a charm
Thanks

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --
Jul 23 '08 #8

P: n/a
En Thu, 24 Jul 2008 12:02:00 -0300, Lanny <la***@freshells.chescribió:
I've used glob.glob to get a list of files in a directory
and now I want to use os.system to execute one of
those files, the problem is that python automatically
puts a escape charater infront of the back slashes
so the os.system gets X:\\####\\####\\ and is useless,
I think I need to convert my string to a raw string but
I don't know how.
Those \\ represent a SINGLE character. That is, they LOOK duplicated in code and when you use repr(...) but it's actually a single backslash:
>>path = "X:\\abc"
path
'X:\\abc'
>>print path
X:\abc
>>len(path)
6

Probably you have another issue - please post a short but complete failing code...

--
Gabriel Genellina

Jul 28 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.