Αποτελέσματα Αναζήτησης
24 Αυγ 2011 · from os import path file_path = path.relpath("2091/data.txt") with open(file_path) as f: <do stuff> should work fine. The path module is able to format a path for whatever operating system it's running on.
13 Ιουν 2019 · In python scripts it works with: file_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'config.ini') But if I need to build a program with py2exe the __file__ doesn't work and I use: file_path = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), 'config.ini') Hope this helps some one.
Opening and Closing a File in Python. When you want to work with a file, the first thing to do is to open it. This is done by invoking the open() built-in function. open() has a single required argument that is the path to the file. open() has a single return, the file object: Python.
25 Ιουλ 2021 · To open a file in Python, Please follow these steps: Find the path of a file. We can open a file using both relative path and absolute path. The path is the location of the file on the disk. An absolute path contains the complete directory list required to locate the file. A relative path contains the current directory and then the file name.
31 Ιαν 2018 · Python 3.4 introduced a new standard library for dealing with files and paths called pathlib — and it’s great! To use it, you just pass a path or filename into a new Path() object using ...
2 ημέρες πριν · os.path. — Common pathname manipulations. ¶. Source code: Lib/genericpath.py, Lib/posixpath.py (for POSIX) and Lib/ntpath.py (for Windows). This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesystem see the os module.
On Windows, paths are written using backslashes (\) as the separator between folder names. On Unix based operating system such as macOS, Linux, and BSDs, the forward slash (/) is used as the path separator. Joining paths can be a headache if your code needs to work on different platforms.