This is the entire code segment. All the functions for file operations are working perfectly. I just need a professional to help me clarify that am following good standards, documentation, etc., because I am going to start doing professional work very soon and I need my code to be at a professional level.
import os
class FileHandle:
def __init__(self, file_path):
self.file_path = file_path
def file_exists(self) -> bool:
"""Determine if a file exists"""
if os.path.exists(self.file_path):
return True
return False
def read_string(self) -> str:
"""Read the string format of a file"""
with open(self.file_path, "r") as current_file_handle:
return current_file_handle.read()
def read_bytes(self) -> bytes:
"""Read the byte format of a file"""
with open(self.file_path, "rb") as current_file_handle:
return current_file_handle.read()
def read_string_lines(self) -> list:
"""Get a list of lines in string format from file"""
with open(self.file_path, "r") as current_file_handle:
return current_file_handle.readlines()
def read_byte_lines(self) -> list:
"""Get a list of lines in byte format from file"""
with open(self.file_path, "rb") as current_file_handle:
return current_file_handle.readlines()
def write_string(self, string: str) -> int:
"""Overwrite the file with a string"""
with open(self.file_path, "w") as current_file_handle:
current_file_handle.write(string)
return 0
def write_bytes(self, string: bytes) -> int:
"""Overwrite the file with bytes"""
with open(self.file_path, "wb") as current_file_handle:
current_file_handle.write(string)
return 0
def append_string(self, string: str) -> int:
"""Add a string to a file"""
with open(self.file_path, "a") as current_file_handle:
current_file_handle.write(string)
return 0
def append_bytes(self, string: bytes) -> int:
"""Add a byte string to a file"""
with open(self.file_path, "ab") as current_file_handle:
current_file_handle.write(string)
return 0
def copy(self, dist: str) -> int:
"""Copy the file to the destination"""
os.system(f'copy "{self.file_path}" {dist}')
if os.path.exists(dist):
return 0
return 1
def move(self, dist: str):
"""Move the file to the destination"""
os.system(f'move "{self.file_path}" {dist}')
if os.path.exists(dist):
return 0
return 1
def delete(self) -> int:
"""Delete the file"""
os.remove(self.file_path)
if self.file_exists():
return 1
return 0
def add_attribute(self, attribute: str):
"""Add attributes to the file
Possible attributes:
1.H - for hidden file
2.A - for archive file
3.R - for readonly file
4.S - for system file
"""
os.system(f"attrib +{attribute.upper()} {self.file_path}")
def remove_attribute(self, attribute: str):
"""Removes attributes from the file
Possible attributes:
1.H - for hidden file
2.A - for archive file
3.R - for readonly file
4.S - for system file
"""
os.system(f"attrib -{attribute.upper()} {self.file_path}")
def rename(self, new_name: str) -> int:
"""Renames the file"""
os.system(f"ren {self.file_path} {new_name}")
if not os.path.exists(new_name):
return 1
return 0
shutil
) for moving and copying files. Thecopy
command doesn't work on Linux, for example, aside from the fact that it is a security risk to run un-validated arguments in a subshell. \$\endgroup\$Args
,Returns
andRaises
sections which tell users what to expect from your function/method/class. \$\endgroup\$