open() function python default directory

后端 未结 7 1473
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 00:23

I\'m new and I have no idea where the default directory for the open() function is.

For example open(\'whereisthisdirectory.txt\',\'r\')

7条回答
  •  难免孤独
    2021-01-05 00:57

    The answer is not python-specific. As with programs written in any other language, the default directory is whatever your operating system considers the current working directory. If you start your program from a command prompt window, that is whatever directory you were in when you ran the program. If you start it from a Windows menu or desktop icon, that directory is usually defined alongside the program's path when creating the icon, or else falls back to some directory that Windows uses in the absence of that information.

    In any case, your program can query the current working directory by calling os.getcwd().

提交回复
热议问题