аватар on5fydu8@gmail.com · 01.01.1970 03:00

Current Directory in Python

as in Python - the program to find out the path to the current directory with the script. I know that this requires an OS library, but there are so many different classes and functions that you can kill half a day in search.

аватар on5fydu8@gmail.com · 01.01.1970 03:00

"

If you start the script c: \ scripts \ script.py from d: \ work folders:

  D: \ Work & Gt; \ Scripts \ Script.py  
  

  • d: \ work & mdash; & nbsp; this is the current working directory at the time of the start of the script. File
  • c: \ scripts & mdash; & nbsp; this is a directory with a script.

Current working directory

Retus, where CWD This is Current Working Directory ("Current Labor Directory" ""). The platforms depends) ITD & mdash; do not invent your ways if you are not aware of all the consequences of a possible change in the behavior of the function. Preferably only --obvious way to do it.

by default relative ways use this directory, therefore obviously call os.getcwd () It is necessary to . os.path.Abspath ('File.txt') & mdash; getcwd () is clearly not used again.

retus the way to the current directory as an object with various useful and convenient methods such as such as .glob ('**/*. Py') .

The directory with a script

The current working directory may differ from the directory with the current python script . os.path.Diame (os.path.abspath (__ fil __)) use to get a directory with the current python script, but this does not always work. Setuptools' instead of building ways using __ File __ . Text:

   import  importlib.resourcestext = Importlib.Resources.Read_text (,    import  app on class # $ pip install Appdirs  user_data_dir = appdirs.user_data_dir (,  "" Who created "" "" "" "" "" "" "" "" " Platforms (Windows, MacOS, Linux) use different agreements,  appdirs  allows you not to produce entities and use suitable directory on each platform. 

Latest

Similar