chdir questions

Fredrik Lundh effbot at
Sun Feb 6 19:25:19 CET 2000

Patrick K Moorman <khadji at> wrote:
> I know this is must be an easy one but how do I use os.chdir?  I have
> many different ways of listing the path but no matter how I type it I get
> error.  Most common is either:
> >>> os.chdir(C:\temp)
>   File "<string>", line 1
>      os.chdir(C:\temp)
>                ^
>  SyntaxError: invalid syntax
> >>> os.chdir(c\temp)
>   File "<string>", line 1
>      os.chdir(c\temp)
>                      ^
>  SyntaxError: invalid token

os.chdir() takes a string as an argument.  to
put a string value in your program, you have
to put quotes around it.  however, string values
treat \ as a special character, so the easiest
way is to use unix-style separators:


alternatively, you can use "raw strings":


or write the backslash as \\


> I have looked through many tut's but it seems that this is too simple to
> mentioned.  Can anyone give the syntax for this including what the path
> should look like?  On a related note, is there a book that has the stanard
> lib broke down like this?  Thank you.

i'd recommend reading the standard tutorial again,
and look at the various code examples a little bit
more carefully:

for more info on strings, start here:
-> Section 3.1.2: Strings

also see the grimoire:

for a full (but formal) description of how python
parses your source program, and how string con-
stants are treated, see:

(since it looks as you haven't used any comparable
programming language, such as C, Java, Basic etc,
you should probably skim the entire chapter).


More information about the Python-list mailing list