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.
Release files for fileaccess 0.2.22
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fileaccess-0.2.22.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fileaccess-0.2.22-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.7 kB
Release files / fileaccess-0.2.22.tar.gz
| Download URL | fileaccess-0.2.22.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
159e57a7e8fb3d4a08c9e53de6d434528d2f2206b5b88dec12adf148910196d7
|
|
BLAKE2b-256 checksum How to use checksums |
15f1ca53c36bc29ec3e3336e8041fce10241d6300e77ab6e0e559da6c556f68d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / fileaccess-0.2.22-py3-none-any.whl
| Download URL | fileaccess-0.2.22-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
59af626e72aac526f1b0a0bc550b2b496e2e2947e04ac8dc1e080e58138dca4c
|
|
BLAKE2b-256 checksum How to use checksums |
170ae873bf4c59e85571c2658017dc1f9cfe9f34a907a878e25acbaed656f4db
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|