A package for easier file access, automatically closes opened file handles, & provides for simple file/directory creation.
Project description
Fileaccess Utility Class
Introduction
Fileaccess is a utility class for safely dealing with files. This class can be used to read, write, or append to a file, as well as create a file or directory. A nice benefit of using it is the automatic file handle management it provides.
The main productivity benefit of using this class is the createFile() method which creates file & directory paths automatically for a given file or directory asset, as well as allowing the writing of file contents together in a single method call.
If you find yourself writing out a directory tree or a lot of files, this can save you time (binary bytes or text are auto-detected & correctly written).
Usage
Reading a File
To read a file, use the with statement & specify the file name & mode as the Fileaccess class parameters. Here's an example:
with Fileaccess("filename.txt", "r") as file:
for line in file:
print(line)
Writing to a File
To write to a file, use the with statement & specify the file name & mode as the Fileaccess class parameters. Here's an example:
with Fileaccess("filename.txt", "w") as file:
file.write("Hello World")
Appending to a File
To append to a file, use the with statement & specify the file name & mode as the Fileaccess class parameters. Here's an example:
with Fileaccess("filename.txt", "a") as file:
file.write("Hello World")
Creating a File or Directory
To create a file or directory, use the createFile method of the Fileaccess class. Here's an example:
Fileaccess.createFile(assetType="f", targetPath="/path/to/file.txt", fileContents="File contents")
In the above example, assetType can be either "f" to create a file or "d" to create a directory. The targetPath parameter specifies the path where the file or directory will be created. The fileContents parameter is optional & can be used to specify the contents of the file.
Note: You do NOT need to first create the directory path for a file if the directory path doesn't exist (all you need are access permissions for the user python is running as). The full directory path will automatically be create for you simply by you providing a full file path.
Note 2: If you wish to exercise more granluar control over file and folder permissions at creation time there are two parameters you may use: filePermissions & folderPermissions
Below are the assumed default values if one or both of these paramters is not given specific other values:
filePermissions="0644"
folderPermissions="0755"
File Access Modes
The mode parameter in the Fileaccess class specifies the file access mode. Here's a table of the available modes:
| Mode | Meaning |
|---|---|
r |
Open for reading (default) |
w |
Open for writing, truncating the file first |
x |
Open for exclusive creation, failing if the file already exists |
a |
Open for writing, appending to the end of file if it exists |
b |
Binary mode |
t |
Text mode (default) |
+ |
Open for updating (reading & writing) |
Error Handling
If an error occurs while using the Fileaccess class, it will be caught & printed to the console. This can be useful for debugging purposes.
License
This code is licensed under the MIT License. See the LICENSE file for more information.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fileaccess-0.2.22.tar.gz.
File metadata
- Download URL: fileaccess-0.2.22.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
159e57a7e8fb3d4a08c9e53de6d434528d2f2206b5b88dec12adf148910196d7
|
|
| MD5 |
1a2d31578845828c090d9e0a66cf7305
|
|
| BLAKE2b-256 |
15f1ca53c36bc29ec3e3336e8041fce10241d6300e77ab6e0e559da6c556f68d
|
File details
Details for the file fileaccess-0.2.22-py3-none-any.whl.
File metadata
- Download URL: fileaccess-0.2.22-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59af626e72aac526f1b0a0bc550b2b496e2e2947e04ac8dc1e080e58138dca4c
|
|
| MD5 |
1f09b60c8e497280cab4a0241f440e58
|
|
| BLAKE2b-256 |
170ae873bf4c59e85571c2658017dc1f9cfe9f34a907a878e25acbaed656f4db
|