About 1,810,000 results
Open links in new tab
  1. Open file in a relative location in Python - Stack Overflow

    Aug 24, 2011 · If you are sure the file you want is in a subdirectory beneath where the script is actually located, you can use __file__ to help you out here. __file__ is the full path to where the script you are running is located. So you can fiddle with something like this: os.path.dirname (file) is not working for me in Python 2.7.

  2. Reading a file using a relative path in a Python project

    But there is an often used trick to build an absolute path from current script: use its __file__ special attribute: test = list(csv.reader(f)) This requires python 3.4+ (for the pathlib module). If you still need to support older versions, you can get the same result with: test = list(csv.reader(f))

  3. Find path to the given file using Python - GeeksforGeeks

    Aug 2, 2024 · We can get the location (path) of the running script file .py with __file__. __file__ is useful for reading other files and it gives the current location of the running file. It differs in versions. In Python 3.8 and earlier, __file__ returns the …

  4. File and directory Paths - Python Cheatsheet

    There are two main modules in Python that deal with path manipulation. One is the os.path module and the other is the pathlib module. The `pathlib` module was added in Python 3.4, offering an object-oriented way to handle file system paths. On Windows, paths are written using backslashes (\) as the separator between folder names.

  5. Working With Files in Python

    Oct 4, 2018 · In this tutorial, you'll learn how you can work with files in Python by using built-in modules to perform practical tasks that involve groups of files, like renaming them, moving them around, archiving them, and getting their metadata.

  6. Browse files and subfolders in Python - Stack Overflow

    May 8, 2012 · In python 3 you can use os.scandir (): for i in os.scandir(path): if i.is_file(): print('File: ' + i.path) elif i.is_dir(): print('Folder: ' + i.path) dir_scan(i.path)

  7. How to Read a File from a Network Path in Python

    Aug 4, 2024 · Python provides several ways to access and read files from these network paths. This guide will walk you through the steps to read a file located on a network path in Windows using Python. Ensure you have Python installed on your system. You can download it from the official Python website.

  8. Understanding File Paths in Python: A Comprehensive Guide

    Mar 17, 2025 · In Python, working with file paths is an essential skill, especially when dealing with tasks such as reading from or writing to files, handling directories, and interacting with the underlying operating system. File paths provide a way to locate and access files and directories on your computer.

  9. Working with File Paths in Python - CodeRivers

    Mar 17, 2025 · In Python programming, working with file paths is an essential skill. Whether you are reading data from a file, writing output to a new file, or managing a project's directory structure, understanding how to handle file paths correctly is crucial.

  10. How to Open Files in Relative Locations using Python

    Nov 6, 2024 · In this guide, we will illuminate practical methodologies to access files via relative paths using Python’s built-in libraries, ensuring that your code operates smoothly across different operating systems. Scenario: Imagine your Python code is executed from a directory called main, and it requires access to the file main/2091/data.txt.

Refresh