473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Raw Strings (I Think)

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
8 1090
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
7
by: Ben | last post by:
Hey everybody, I'm working on a program in c++, and I've come up against a problem that I can't figure out. I don't imagine that it's too difficult to solve, but it has been giving me trouble....
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
73
by: Rigga | last post by:
Hi all, I am wondering why string's are not true objects?.... Let me explain... If i write the code Dim x1 as String = "veg" Dim x2 as String = "veg" If x1 = x2 then
74
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.