|
| 1 | +import os |
| 2 | +import time |
| 3 | + |
| 4 | + |
| 5 | +def create_file(file_name, *args): # *args is in case we receive more than one parameter |
| 6 | + try: |
| 7 | + ''' Delete file content ''' |
| 8 | + |
| 9 | + os.remove(file_name) # removing file |
| 10 | + file = open(file_name, "x") # creating file |
| 11 | + file.close() # closing file |
| 12 | + print(f"File {file_name} content was deleted") # comment that will show on the console if the content is deleted |
| 13 | + |
| 14 | + except FileNotFoundError: |
| 15 | + ''' Create file ''' |
| 16 | + |
| 17 | + file = open(file_name, "x") # creating file |
| 18 | + file.close() # closing file |
| 19 | + print(f"File {file_name} was created") # comment, that will show on the console, if the creation was successful |
| 20 | + |
| 21 | + |
| 22 | +def add_content(file_name, *args_content): # *args_content is in case we receive more than two parameters |
| 23 | + content = args_content[0] |
| 24 | + |
| 25 | + if os.path.exists(file_name): |
| 26 | + print(f"Content added to {file_name}") # comment that will show on the console if the file exists |
| 27 | + |
| 28 | + else: |
| 29 | + print(f"Created {file_name} and added content") # comment that will show, if the file does not exist |
| 30 | + |
| 31 | + file = open(file_name, "a+") # open file if it exists, else it creates the file. |
| 32 | + file.write(f"{content}\n") # adding the content |
| 33 | + file.close() # closing file |
| 34 | + |
| 35 | + |
| 36 | +def replace_content(file_name, *content): # *content is in case we receive more than two parameters |
| 37 | + old_content, new_content, = content[0], content[1] |
| 38 | + |
| 39 | + try: |
| 40 | + |
| 41 | + file = open(file_name, "r") # open for reading |
| 42 | + lines = [line for line in file] # read lines |
| 43 | + file.close() # close for reading |
| 44 | + |
| 45 | + for index, line in enumerate(lines): |
| 46 | + line = line.replace(old_content, new_content) # replace all occurrences |
| 47 | + lines[index] = line |
| 48 | + |
| 49 | + os.remove(file_name) # remove file |
| 50 | + file = open(file_name, "x") # create file (delete content) |
| 51 | + file.close() # close file |
| 52 | + |
| 53 | + file = open(file_name, "a+") # open for writing |
| 54 | + [file.write(line) for line in lines] # rewrite the modified content |
| 55 | + file.close() # close for writing |
| 56 | + |
| 57 | + print(f"{file_name}'s content successfully changed") # if everything is successful, this will show on the console |
| 58 | + |
| 59 | + except FileNotFoundError: |
| 60 | + print("An error occurred", "# replacing content") |
| 61 | + |
| 62 | + |
| 63 | +def delete_file(file_name, *args): # *args is in case we receive more than one parameter |
| 64 | + if os.path.exists(file_name): |
| 65 | + os.remove(file_name) |
| 66 | + print(f"File {file_name} was removed") # this will show on the console, if the file is removed successfully |
| 67 | + else: |
| 68 | + print("An error occurred", "# Cannot delete file that does not exist") # If the file does not exists |
| 69 | + |
| 70 | + |
| 71 | +operations = { |
| 72 | + "Create": create_file, |
| 73 | + "Add": add_content, |
| 74 | + "Replace": replace_content, |
| 75 | + "Delete": delete_file |
| 76 | +} |
| 77 | + |
| 78 | +command = input() |
| 79 | +while command != "End": |
| 80 | + |
| 81 | + operation, name_of_file, *more_content = command.split("-") |
| 82 | + |
| 83 | + operations[operation](name_of_file, *more_content) |
| 84 | + |
| 85 | + time.sleep(0.1) # using time.sleep, so the output will be ordered, otherwise it somehow gets mixed up |
| 86 | + |
| 87 | + command = input() |
0 commit comments