What exactly is Current working directory?

后端 未结 4 910
一整个雨季
一整个雨季 2021-01-20 16:10

My book states:

Every program that runs on your computer has a current working directory, or cwd. Any filenames or paths that do not begin with the r

相关标签:
4条回答
  • 2021-01-20 16:37

    os.getcwd() has nothing to do with OSX in particular. It simply returns the directory/location of the source-file. If my source-file is on my desktop it would return C:\Users\Dave\Desktop\ or let say the source-file is saved on an external storage device it could return something like G:\Programs\. It is the same for both unix-based and Windows systems.

    0 讨论(0)
  • 2021-01-20 16:43

    Your python interpreter location is based off of how you launched it, as well as subsequent actions taken after launching it like use of the os module to navigate your file system. Merely starting the interpreter will place you in the directory of your python installation (not the same on different operating systems). On the other hand, if you start by editing or running a file within a specific directory, your location will be the folder of the file you were editing. If you need to run the interpreter in a certain directory and you are using idle for example, it is easiest to start by creating a python file there one way or another and when you edit it you can start a shell with Run > Python Shell which will already be in that directory. If you are using the command line interpreter, navigate to the folder where you want to run your interpreter before running the python/python3/py command. If you need to navigate manually, you can of course use the following which has already been mentioned:

    import os
    os.chdir('full_path_to_your_directory')
    
    0 讨论(0)
  • 2021-01-20 16:50

    This has nothing to do with osx in particular, it's more of a concept shared by all unix-based systems, and I believe Windows as well. os.getcwd() is the equivalent of the bash pwd command - it simply returns the full path of the current location in which you are in. In other words:

    alex@suse:~> cd /
    alex@suse:/> python
    Python 2.7.12 (default, Jul 01 2016, 15:34:22) [GCC] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.getcwd()
    '/'
    

    It depends from where you started the python shell/script.

    0 讨论(0)
  • 2021-01-20 16:55

    Python is usually (except if you are working with virtual environments) accessible from any of your directory. You can check the variables in your path and Python should be available. So the directory you get when you ask Python is the one in which you started Python. Change directory in your shell before starting Python and you will see you will it.

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