About 583,000 results
Open links in new tab
  1. python - Find the current directory and file's directory - Stack …

    To get the full path to the directory a Python file is contained in, write this in that file: import os dir_path = os.path.dirname(os.path.realpath(__file__)) (Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current ...

  2. python - How do I get the full path of the current file's directory ...

    OUTPUT: ABSOLUTE PATH IS THE PATH WHERE YOUR PYTHON FILE IS PLACED. Absolute path : D:\Study\Machine Learning\Jupitor Notebook\JupytorNotebookTest2\Udacity_Scripts\Matplotlib and seaborn Part2. File path : D:\Study\Machine Learning\Jupitor Notebook\JupytorNotebookTest2\Udacity_Scripts\Matplotlib and seaborn Part2\data\fuel_econ.csv. isfileExist ...

  3. How should I write a Windows path in a Python string literal?

    Apr 9, 2023 · However, the best practice is to use the os.path module functions that always joins with the correct path separator (os.path.sep) for your OS: os.path.join(mydir, myfile) From python 3.4 you can also use the pathlib module. This is equivalent to the above: pathlib.Path(mydir, myfile) or: pathlib.Path(mydir) / myfile

  4. Open file in a relative location in Python - Stack Overflow

    Aug 24, 2011 · Remove the last item in the list (the actual script file): script_directory = path_list[0:len(path_list)-1] Add the relative file's path: rel_path = "main/2091/data.txt Join the list items, and addition the relative path's file: path = "/".join(script_directory) + "/" + rel_path

  5. python - How can I create a full path to a file from parts (e.g. path ...

    suffix = '.pdf' os.path.join(dir_name, base_filename + suffix) That approach also happens to be compatible with the suffix conventions in pathlib, which was introduced in python 3.4 a few years after this question was asked. New code that doesn't require backward compatibility can do this:

  6. How to get an absolute file path in Python - Stack Overflow

    Sep 9, 2008 · Given a path such as mydir/myfile.txt, how do I find the file's absolute path relative to the current working directory in Python? I would do it like this, import os.path os.path.join( os.getcwd(), 'mydir/myfile.txt' )

  7. Extract a part of the filepath (a directory) in Python

    Mar 20, 2021 · import os ## first file in current dir (with full path) file = os.path.join(os.getcwd(), os.listdir(os.getcwd())[0]) file os.path.dirname(file) ## directory of file os.path.dirname(os.path.dirname(file)) ## directory of directory of file ... And you can continue doing this as many times as necessary...

  8. python - How do I list all files of a directory? - Stack Overflow

    Jul 9, 2010 · def list_files(path): # returns a list of names (with extension, without full path) of all files # in folder path files = [] for name in os.listdir(path): if os.path.isfile(os.path.join(path, name)): files.append(name) return files

  9. python - How to set the current working directory? - Stack Overflow

    @jwodder - I agree with you. OTOH, there are at least 24 people for which this was useful. Perhaps it was the fact that he covered ítems in the comments of the accepted answer: 1) format of explicit paths, 2) how to get examples of such (with getcwd).... remarkable.

  10. python - How do I create a file at a specific path? - Stack Overflow

    Feb 24, 2011 · The besty practice is to use '/' and a so called 'raw string' to define file path in Python. path = r"C:/Test.py" However, a normal program may not have the permission to write in the C: drive root directory. You may need to allow your program to do so, or choose something more reasonable since you probably not need to do so.

Refresh