Skip to content

Commit

Permalink
Merge pull request #2 from JeelDobariya38/Dev
Browse files Browse the repository at this point in the history
add function to save and load int and list
  • Loading branch information
JeelDobariya38 authored Jun 10, 2023
2 parents dc645a8 + a7c705e commit a7ae829
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
127 changes: 127 additions & 0 deletions pyfilehandling/TypeFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
def save_int(path: str,data: int,mode: str ="w") -> bool:

"""Writes data to a file.

Args:

path (str): The path of the file.

data (str): The data to be written.

mode (str, optional): The file opening mode. Defaults to "w" (write mode).

Returns:

bool: True if the write operation is successful, False otherwise.

"""

with open(path, mode) as f:

f.write(str(data))

return True

def load_int(path: str) -> int:

"""Reads the contents of a file.

Args:

path (str): The path of the file.

Returns:

int: Integer stored in file.

"""

with open(path, "r") as f:

return int(f.read())



def save_list(path: str,data: list,mode: str='w') -> bool:

"""Writes data to a file.

Args:

path (str): The path of the file.

data (str): The data to be written.

mode (str, optional): The file opening mode. Defaults to "w" (write mode).

Returns:

bool: True if the write operation is successful, False otherwise.

"""

with open(path,mode) as f:

for entry in data:

f.write(str(entry) + '\n')

return True

def load_list(path: str) -> list:

"""Reads the contents of a file.

and return a list of content

Args:

path (str): The path of the file.

Returns:

int: Integer stored in file.

"""

data = []

with open(path, "r") as f:

for text in f.readlines():

text = text[0:-1]

if text == str(True) or text == str(False):

if text == str(True):

data.append(True)

else:

data.append(False)

elif text.startswith('[') and text.endswith(']'):

data.append(text)

elif text.startswith('(') and text.endswith(')'):

data.append(text)

elif text.startswith('{') and text.endswith('}'):

data.append(text)

elif not text.isalpha():

text = float(text)

data.append(text)

else:

data.append(text)

return data
2 changes: 1 addition & 1 deletion pyfilehandling/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@

from pyfilehandling.FileIO import create,write,read
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='pyfilehandling',
version='1.0',
version='1.1',
description='A Python package for file manipulation operations.',
long_description='pyfilehandling is a Python package that provides functions and classes for handling file operations, including creating files and directories, reading and writing data, and more.',
long_description_content_type='text/markdown',
Expand Down

0 comments on commit a7ae829

Please sign in to comment.