How to know/change current directory in Python shell?

后端 未结 7 1978
无人及你
无人及你 2020-11-27 09:27

I am using Python 3.2 on Windows 7. When I open the Python shell, how can I know what the current directory is and how can I change it to another directory where my modules

相关标签:
7条回答
  • 2020-11-27 10:05

    Changing the current directory is not the way to deal with finding modules in Python.

    Rather, see the docs for The Module Search Path for how Python finds which module to import.

    Here is a relevant bit from Standard Modules section:

    The variable sys.path is a list of strings that determines the interpreter’s search path for modules. It is initialized to a default path taken from the environment variable PYTHONPATH, or from a built-in default if PYTHONPATH is not set. You can modify it using standard list operations:

    >>> import sys
    >>> sys.path.append('/ufs/guido/lib/python')

    In answer your original question about getting and setting the current directory:

    >>> help(os.getcwd)
    
    getcwd(...)
        getcwd() -> path
    
        Return a string representing the current working directory.
    
    >>> help(os.chdir)
    
    chdir(...)
        chdir(path)
    
        Change the current working directory to the specified path.
    
    0 讨论(0)
提交回复
热议问题