enow.com Web Search

Search results

  1. Results from the WOW.Com Content Network
  2. 37. Answer to #1: If you want the current directory, do this: import os. os.getcwd() If you want just any folder name and you have the path to that folder, do this: def get_folder_name(folder): '''. Returns the folder name, given a full folder path.

  3. Aug 9, 2017 at 13:03. cwd stands for current working directory same as like pwd in linux. there is, os.getcwd() will give you the directory name in which you are executing it. – ggupta. Aug 9, 2017 at 13:05. Yes, if you ask Python what its cwd is and it says that it's the Documents directory, then the cwd is the Documents directory.

  4. 73. import os. print os.getcwd() # Prints the current working directory. To set the working directory: os.chdir('c:\\Users\\uname\\desktop\\python') # Provide the new path here. edited Oct 26, 2018 at 7:36. Community Bot. 1 1. answered Jan 24, 2016 at 3:41.

  5. 112. In Python 3.x I do: from pathlib import Path. path = Path(__file__).parent.absolute() Explanation: Path(__file__) is the path to the current file. .parent gives you the directory the file is in. .absolute() gives you the full absolute path to it. Using pathlib is the modern way to work with paths.

  6. How can I list the contents of a directory in Python?

    stackoverflow.com/questions/2759323

    Since Python 3.5, you can use os.scandir.. The difference is that it returns file entries not names. On some OSes like windows, it means that you don't have to os.path.isdir/file to know if it's a file or not, and that saves CPU time because stat is already done when scanning dir in Windows:

  7. 304. This will change your current working directory to so that opening relative paths will work: import os. os.chdir("/home/udi/foo") However, you asked how to change into whatever directory your Python script is located, even if you don't know what directory that will be when you're writing your script.

  8. How to know/change current directory in Python shell?

    stackoverflow.com/questions/8248397

    The easiest way to change the current working directory in python is using the 'os' package. Below there is an example for windows computer: # Import the os package. import os. # Confirm the current working directory. os.getcwd() # Use '\\' while changing the directory. os.chdir("C:\\user\\foldername") edited Dec 4, 2018 at 12:22.

  9. how do you get the current local directory in python

    stackoverflow.com/questions/19986300

    The cross-platform way of getting the name of the directory you're in would be. import os. cwd = os.getcwd() # use os.path.basename instead of your own function! print(os.path.basename(cwd)) # Evaluates to True if you have Unix-y path separators: try it out! os.path.basename(cwd) == cwd.split('/')[-1] >>> True.

  10. This is slightly risky. If __file__ is a relative filename relative to the current working directory (e.g., setup.py), then os.path.dirname(__file__) will be the empty string. For this and similar concerns raised by John Jiang, ekhumoro's more general-purpose solution is strongly preferable. –

  11. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.