diff --git a/cartesian_plane_quadrant.py b/cartesian_plane_quadrant.py index 606aec8..b4d64a4 100644 --- a/cartesian_plane_quadrant.py +++ b/cartesian_plane_quadrant.py @@ -11,8 +11,8 @@ def determine_quadrant(x, y): return 'III(-,-)' elif x > 0 and y < 0 : return 'IV(+,-)' - else : - return 'Invalid parameters were provided') + else: + return ('Invalid parameters were provided') except TypeError: return "X and Y co-ords must be integers and not X {}, Y{}".format(type(x), type(y)) diff --git a/readFiles.py b/readFiles.py index 73948ea..ac2b78f 100644 --- a/readFiles.py +++ b/readFiles.py @@ -1,7 +1,34 @@ -# Reading files +# Reading a file in a directory +from pathlib import Path -# Enter file name which is in same directory as that of the program -fileName = str(input('File name : ')) -fileToRead = open(fileName, 'r') # 'r' reads the file -print(fileToRead.read()) # reading file -fileToRead.close() # closing the file +#check if valid directory +while True: + try: + d = str(input('Please enter a directory path: ')) + d_path = Path(d) + if d_path.is_dir(): + break + else: + raise ValueError + except (ValueError, OSError): + print('Specified directory path not found') + +#show all files in directory chosen +lst = [] +for obj in d_path.iterdir(): + if obj.is_file(): + lst.append(obj) +# sorting lexicographically taking each path object as a string +lst_copy = sorted(lst, key=lambda obj: str(obj)) +for obj in lst_copy: + print(obj) + +#open file +while True: + try: + file = str(input('Please enter a file name: ')) + for line in open(file): #more memory efficient + line = line.rstrip('\n') + print(line) + except (OSError, FileNotFoundError): + print('File error') \ No newline at end of file diff --git a/writingFiles.py b/writingFiles.py index 849628b..9d94644 100644 --- a/writingFiles.py +++ b/writingFiles.py @@ -1,8 +1,36 @@ -# Writing files - -# Enter file name which is in same directory as that of the program -fileName = str(input('File name : ')) -fileToWrite = open(fileName, 'w') # 'w' writes to the file -textToWrite = str(input('Text to write : ')) -fileToWrite.write(textToWrite) # writing file -fileToWrite.close() # closing the file +# Writing to a file in a directory +from pathlib import Path + +#check if valid directory +while True: + try: + d = str(input('Please enter a directory path: ')) + d_path = Path(d) + if d_path.is_dir(): + break + else: + raise ValueError + except (ValueError, OSError): + print('Specified directory path not found') + +#show all files in directory chosen +lst = [] +for obj in d_path.iterdir(): + if obj.is_file(): + lst.append(obj) +# sorting lexicographically taking each path object as a string +lst_copy = sorted(lst, key=lambda obj: str(obj)) +for obj in lst_copy: + print(obj) + +#open file +while True: + try: + file = str(input('Please enter a file name: ')) + fileToWrite = open(file, 'w') # 'w' writes to the file + textToWrite = str(input('Text to write: ')) + fileToWrite.write(textToWrite) # writing file + fileToWrite.close() # closing the file + except (OSError, FileNotFoundError): + print('File error') +