Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
common-package
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GSHS Utilities
common-package
Merge requests
!29
Develop
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
Develop
develop
into
main
Overview
0
Commits
153
Pipelines
0
Changes
21
Closed
Huihui, Jonathan
requested to merge
develop
into
main
1 year ago
Overview
0
Commits
153
Pipelines
0
Changes
21
Expand
0
0
Merge request reports
Viewing commit
3dab43bb
Prev
Next
Show latest version
21 files
+
1067
−
1
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
21
Search (e.g. *.vue) (Ctrl+P)
3dab43bb
Added scrapper to code base
· 3dab43bb
Jacob
authored
11 months ago
src/common/file_operations.py
0 → 100644
+
163
−
0
Options
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Provide common utilities for various file operations.
"""
import
json
from
pathlib
import
Path
# So, here are the tasks for file operations
# Use Pathlib package
# Create/Delete directory/folder
# Tree command/function to walk a directory
# Read/Append/Delete file -- default format text
# for Json files - use json module
# append a key/dict/etc to the JSON would be nice
def
path_exists
(
dirc_or_file
):
"""
# function to check if directory/file exists
# @params : directory/file to check
# @returns : boolean
"""
return
Path
(
dirc_or_file
).
exists
()
def
create_directory
(
dirc
=
None
):
"""
# function to create directories at the specified path
# @params : directory to create
# @returns : boolean / None
"""
if
dirc
is
not
None
:
try
:
if
path_exists
(
dirc
):
return
f
"
{
dirc
}
already exists.
"
else
:
Path
(
dirc
).
mkdir
()
return
True
except
FileNotFoundError
as
e
:
print
(
f
"
Error:
{
e
}
"
)
return
None
def
delete_directory
(
dirc
=
None
):
"""
# function to delete directories at the specified path
# @params : directory to delete
# @returns : str / None
"""
if
dirc
is
not
None
:
try
:
if
path_exists
(
dirc
):
directory_to_delete
=
Path
(
dirc
)
for
item
in
directory_to_delete
.
iterdir
():
if
item
.
is_file
():
item
.
unlink
()
if
item
.
is_dir
():
item
.
rmdir
()
directory_to_delete
.
rmdir
()
return
f
"
{
dirc
}
deleted successfully.
"
else
:
return
f
"
{
dirc
}
does not exist.
"
except
FileNotFoundError
as
e
:
print
(
f
"
Error:
{
e
}
"
)
return
None
def
tree
(
dirc
=
None
):
"""
# function to walk a directory
# @params : directory to walk
# @returns : None
"""
if
dirc
is
not
None
:
try
:
if
path_exists
(
dirc
):
for
item
in
Path
(
dirc
).
glob
(
"
*
"
):
if
item
.
is_file
():
print
(
f
"
File:
{
item
}
"
)
elif
item
.
is_dir
():
print
(
f
"
Directory:
{
item
}
"
)
else
:
return
f
"
{
dirc
}
does not exist.
"
except
Exception
as
e
:
print
(
f
"
Error:
{
e
}
"
)
return
None
def
read_file
(
file_name
):
"""
# function to read a file
# @params : file name
# @returns : str
"""
try
:
with
open
(
f
"
{
file_name
}
"
,
"
r+
"
)
as
file
:
if
Path
(
file_name
).
suffix
==
"
.json
"
:
file_contents
=
json
.
load
(
file
)
else
:
file_contents
=
file
.
read
()
return
file_contents
except
FileNotFoundError
as
e
:
print
(
f
"
File doesn
'
t exist:
{
e
}
"
)
except
json
.
JSONDecodeError
as
e
:
print
(
f
"
Invalid JSON file:
{
e
}
"
)
return
None
def
append_file
(
file_name
,
file_data
):
"""
# function to append contents to a file
# @params : file name, data to append
# @returns : str / None
# append_file function will not work for JSON files as key/value pairs are unordered.
"""
try
:
with
open
(
f
"
{
file_name
}
"
,
"
a+
"
)
as
file
:
file_contents
=
file
.
write
(
file_data
)
return
f
"
Contents successfully appended to the file
"
except
FileNotFoundError
as
e
:
print
(
f
"
File doesn
'
t exist:
{
e
}
"
)
except
TypeError
as
te
:
print
(
f
"
Error:
{
te
}
, please provide data as string
"
)
return
None
def
write_file
(
file_name
,
file_data
=
""
):
"""
# function to write to a file
# @params : file name, data to write
# @returns : None
"""
try
:
if
Path
(
file_name
).
suffix
==
"
.json
"
:
if
not
Path
(
file_name
).
exists
():
with
open
(
f
"
{
file_name
}
"
,
"
w
"
)
as
json_file
:
json_file
.
write
(
json
.
dumps
({}))
with
open
(
f
"
{
file_name
}
"
,
"
r+
"
)
as
file
:
file_contents
=
json
.
load
(
file
)
file_contents
.
update
(
file_data
)
file
.
seek
(
0
)
json
.
dump
(
file_contents
,
file
,
indent
=
1
)
else
:
with
open
(
file_name
,
"
w
"
)
as
file
:
file
.
write
(
file_data
)
except
FileNotFoundError
as
e
:
print
(
f
"
File doesn
'
t exist:
{
e
}
"
)
except
TypeError
as
te
:
print
(
f
"
Error:
{
te
}
, please provide data as string
"
)
return
None
def
delete_file
(
file_name
):
"""
# function to delete a file
# @params : file name
# @returns : str
"""
if
Path
(
file_name
).
exists
():
Path
(
file_name
).
unlink
()
return
f
"
File:
{
file_name
}
deleted successfully.
"
return
f
"
Error in deleting the file:
{
file_name
}
.
"
Loading