-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create file_utils.py #2735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Create file_utils.py #2735
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
35faf3c
Create file_utils.py
MonarchChakri 5d9f968
Update file_utils.py
MonarchChakri d15d4af
Update other/file_utils.py
MonarchChakri dc47da2
Update other/file_utils.py
MonarchChakri 4078ae2
Added type hints, docstrings and removed prints
MonarchChakri fd07da2
Update file_utils.py
MonarchChakri 9398b70
Update file_utils.py
MonarchChakri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,129 @@ | ||
import json | ||
import sys | ||
|
||
|
||
def touch_file(file_name): | ||
try: | ||
with open(file_name): | ||
pass | ||
except FileNotFoundError: | ||
try: | ||
with open(file_name, "w"): | ||
pass | ||
except IOError as ioe: | ||
print(ioe) | ||
sys.stdout.flush() | ||
print("Unable to create file {}".format(file_name)) | ||
sys.stdout.flush() | ||
except IOError as ioe: | ||
print(ioe) | ||
sys.stdout.flush() | ||
print("Unable to read file {}".format(file_name)) | ||
sys.stdout.flush() | ||
|
||
|
||
def clear_file(file_name): | ||
try: | ||
with open(file_name, "w"): | ||
pass | ||
except IOError as ioe: | ||
print(ioe) | ||
sys.stdout.flush() | ||
print("Unable to create file {}".format(file_name)) | ||
sys.stdout.flush() | ||
|
||
|
||
def dump_json_to_file(data, file_name): | ||
touch_file(file_name) | ||
with open(file_name, "w") as write_file: | ||
write_file.write(json.dumps(data, indent=4)) | ||
write_file.write("\n") | ||
MonarchChakri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def load_json_from_file(file_name): | ||
touch_file(file_name) | ||
with open(file_name) as readfile: | ||
content = readfile.read() | ||
if content == "": | ||
content = str(dict()) | ||
content = json.loads(content) | ||
return content | ||
MonarchChakri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def read_lines_strip_return(file_name, split=None, index=None): | ||
try: | ||
with open(file_name) as readfile: | ||
lines = readfile.readlines() | ||
res = list() | ||
for line in lines: | ||
line = line.strip() | ||
if line != "": | ||
if split is not None: | ||
line = line.split(split) | ||
if index is None: | ||
res.append(line) | ||
else: | ||
res.append(line[index]) | ||
else: | ||
res.append(line) | ||
return res | ||
except FileNotFoundError: | ||
return "FileNotFoundError: {0}".format(file_name) | ||
except IOError as ioe: | ||
print(ioe) | ||
sys.stdout.flush() | ||
print("Unable to read file {}".format(file_name)) | ||
sys.stdout.flush() | ||
|
||
|
||
def read_file(file_name): | ||
try: | ||
with open(file_name) as readfile: | ||
return readfile.read().strip() | ||
except FileNotFoundError: | ||
return "FileNotFoundError: {0}".format(file_name) | ||
except IOError as ioe: | ||
print(ioe) | ||
sys.stdout.flush() | ||
print("Unable to read file {}".format(file_name)) | ||
sys.stdout.flush() | ||
|
||
|
||
def append_line_to_file(file_name, content): | ||
touch_file(file_name) | ||
with open(file_name, "a") as append_file: | ||
append_file.write(content + "\n") | ||
|
||
|
||
def append_line_to_file_if_doesnt_exist(file_name, content): | ||
touch_file(file_name) | ||
lines = read_lines_strip_return(file_name) | ||
if content not in lines: | ||
with open(file_name, "a") as append_file: | ||
append_file.write(content + "\n") | ||
|
||
|
||
def check_file_exists(file_name): | ||
try: | ||
with open(file_name): | ||
pass | ||
return True | ||
except FileNotFoundError: | ||
return False | ||
except IOError as ioe: | ||
print(ioe) | ||
sys.stdout.flush() | ||
print("Unable to read file {}".format(file_name)) | ||
sys.stdout.flush() | ||
return False | ||
|
||
|
||
def convert_int_to_dotted_str(number): | ||
res = list() | ||
for digit in str(number): | ||
res.append(digit) | ||
return ".".join(res) | ||
|
||
|
||
def convert_dotted_str_to_int(dotted_str): | ||
return int("".join(dotted_str.split("."))) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.