change current working directory in python

前端 未结 2 1907
挽巷
挽巷 2021-02-20 04:17

I made a folder on my desktop with the name \"headfirstpython\" and I need to change my current working directory to that folder and to the sub folder inside of it. I used os.ge

相关标签:
2条回答
  • 2021-02-20 05:07

    I think a few things may be helpful.

    It looks like you're on a windows system, so you should use double back slashes '\\' to separate the folders.

    Second, if you're trying to change to a folder within the current folder, you should use a single dot, and not two, e.g. os.chdir('.\\folder')

    Finally, if the folder you are trying to access is not a direct subfolder of the current working directory (or otherwise in your path), you need to include the full path to access it. Since you said it's on your desktop, you'd probably want something that looks like this:

    import os
    os.chdir('C:\\Users\\username\\Desktop\\headfirstpython') ## Where username is replaced with your actual username
    

    From here, you could also change directories to the chapter3 subdirectory with the following

    os.chdir('chapter3') 
    

    Which is equivalent in this case with

    os.chdir('.\\chapter3')
    

    or, if you want to be wordy:

    os.chdir('C:\\Users\\username\\Desktop\\headfirstpython\\chapter3')
    

    Hopefully that helps?

    0 讨论(0)
  • 2021-02-20 05:15

    I had the same problem before.I solve this when I found that if I created a file on my desktop, the file image would be shown on my desktop, but it will not exist in C/users/Desktop. Maybe you can check whether your file is exist in your C drive's desktop or not. Hope this will help.

    0 讨论(0)
提交回复
热议问题