chdir questions

Fredrik Lundh effbot at telia.com
Sun Feb 6 13:25:19 EST 2000


Patrick K Moorman <khadji at pld.com> wrote:
> I know this is must be an easy one but how do I use os.chdir?  I have
tried
> many different ways of listing the path but no matter how I type it I get
an
> 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:

    os.chdir("C:/temp")

alternatively, you can use "raw strings":

    os.chdir(r"C:\temp")

or write the backslash as \\

    os.chdir("C:\\temp")

> I have looked through many tut's but it seems that this is too simple to
be
> 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:
http://www.python.org/doc/current/tut

for more info on strings, start here:
http://www.python.org/doc/current/tut/node5.html
-> Section 3.1.2: Strings

also see the grimoire:
http://starship.python.net/crew/amk/grimoire

for a full (but formal) description of how python
parses your source program, and how string con-
stants are treated, see:
http://www.python.org/doc/current/ref/lexical.html

(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).

</F>





More information about the Python-list mailing list