PDA

View Full Version : A little Python question



C++buntu
July 27th, 2009, 02:41 AM
Hi everyone,

I'm using IPython on Windows to learn a little Python.
I want to use the os module to be able to change the working directory...



import os
os.chdir(r'C:\Documents and Settings\My name\My Documents\Scientific Python\')
---------------------------------------------------
File "<ipython console>, line 1
os.chdir(r'C:\Documents and Settings\My name\My Documents\Scientific Python\')
little cap here


What i'm doing wrong?

Thanks

shadylookin
July 27th, 2009, 03:54 AM
not sure with python but some languages uses \ as an escape character so when you want to use \ you have to use \\

for instance

C:\Stuff\more stuff\

needs to be written as

C:\\Stuff\\more stuff\\

could be wrong about this haven't used python in a long time

slavik
July 27th, 2009, 03:57 AM
use raw string?

lavinog
July 27th, 2009, 04:00 AM
not sure with python but some languages uses \ as an escape character so when you want to use \ you have to use \\

for instance

C:\Stuff\more stuff\

needs to be written as

C:\\Stuff\\more stuff\\

could be wrong about this haven't used python in a long time

the r at the beginning of the string is for a raw string with no escaping.

@C++buntu: Windows will not let you cd to a directory with spaces without using quotes.
try


os.chdir(r'"c:\Documents and Settings\My name\My Documents\Scientific Python\"')

days_of_ruin
July 27th, 2009, 04:08 AM
Some good info here: http://bytes.com/topic/python/answers/722075-windows-xp-os-chdir-path-problem

Can+~
July 27th, 2009, 04:32 AM
I remember reading that Windows accepts forward slashes also. Long time without using windows though.

But yeah, "raw string" is the easiest way to go.