-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add file extension (read/write)
Signed-off-by: FreehuntX <freehuntx@web.de>
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# File extension | ||
|
||
Author: [FreehuntX](https://github.com/freehuntx) | ||
|
||
Helper functions to read/write files. | ||
|
||
## Usage | ||
|
||
### Read a file as text | ||
|
||
```python | ||
load('ext://file', 'read_file') | ||
|
||
text = read_file('/tmp/some_file') | ||
print(text) # Prints the content of /tmp/some_file | ||
``` | ||
|
||
### Write text to a file | ||
|
||
```python | ||
load('ext://file', 'write_file') | ||
|
||
write_file('/tmp/some_file', 'This is some text') # Writes to /tmp/some_file | ||
print(read_file('/tmp/some_file')) # Prints the content | ||
``` | ||
|
||
## Functions | ||
### read_file(path) | ||
#### Parameters | ||
* `path` (str) – The path of the file to read | ||
#### Result | ||
* (str) - The content of the file | ||
### write_file(path, content) | ||
#### Parameters | ||
* `path` (str) – The path of the file to read | ||
* `content` (str) – The text to write to the file | ||
#### Result | ||
* (void) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def read_file(path): | ||
if os.name == "posix": # Linux/Mac implementation | ||
return str(local("cat {}".format(path), echo_off=True, quiet=True)) | ||
elif os.name == "nt": # Windows implementation | ||
path = path.replace("/", "\\") | ||
return str(local("type {}".format(path), echo_off=True, quiet=True)) | ||
else: | ||
fail("Unknown system architecture: " + os.name) | ||
|
||
def write_file(path, content): | ||
content = str(content) | ||
|
||
if os.name == "posix": # Linux/Mac implementation | ||
local("echo '{}' > {}".format(content, path), echo_off=True, quiet=True) | ||
elif os.name == "nt": # Windows implementation | ||
path = path.replace("/", "\\") | ||
local('type nul > {}'.format(path), echo_off=True, quiet=True) | ||
for line in content.splitlines(): | ||
local('echo {} >> {}'.format(str(line).replace('&', '^&').replace('<', '^<').replace('>', '^>'), path), echo_off=True, quiet=True) | ||
else: | ||
fail("Unknown system architecture: " + os.name) |