问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/18239915/python-os-chdir-doesnt-seem-to-work