简体   繁体   中英

Python os.chdir() doesn't seem to work

I can't seem to change my directory in python:

import os

os.getcwd()

'C:\\Users\\Jon\\Folder\\IdbyGenotype'

os.chdir(r"C:\Users\Jon\Folder\IdbyGenotype\thisone")

os.getcwd()

'C:\\Users\\Jon\\Folder\\IdbyGenotype'

Am I missing something? What could be going wrong here?

Thanks

Use forward-slash(/) in path whether you are using Windows or Linux

import os

os.getcwd()

'C:\\Users\\Jon\\Folder\\IdbyGenotype'

os.chdir("C:/Users/Jon/Folder/IdbyGenotype/thisone")

This worked in my case.

 import os
 os.getcwd()
 'C:\\Program Files\\PYTHON'
 os.chdir('c:\\mytemp')
 os.getcwd()
 'c:\\mytemp'
 os.chdir(r'c:\')
 SyntaxError: EOL while scanning string literal
 os.chdir(r"c:\\")
 os.getcwd()
 'c:\\'

I have had inconsistent results with the use of r to communicate that the following is a raw string. As you can see when I use the r I get an error when I hit enter.

Thus have you tried to use escaped-backslashes in your os.chdir() command?

You are implying that you did not receive any error messages - this is strange because I get error messages when I try to chdir to a directory that I do not have rights to under my user name and when I try to chdir to a directory that does not exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM