|
| 1 | +""" |
| 2 | +File handling is an important part of any web application. |
| 3 | +
|
| 4 | +Python has several functions for creating, reading, updating, and deleting files. |
| 5 | +
|
| 6 | +File Handling |
| 7 | +The key function for working with files in Python is the open() function. |
| 8 | +The open() function takes two parameters; filename, and mode. |
| 9 | +There are four different methods (modes) for opening a file: |
| 10 | +
|
| 11 | +"r" - Read - Default value. Opens a file for reading, error if the file does not exist |
| 12 | +"a" - Append - Opens a file for appending, creates the file if it does not exist |
| 13 | +"w" - Write - Opens a file for writing, creates the file if it does not exist |
| 14 | +"x" - Create - Creates the specified file, returns an error if the file exists |
| 15 | +
|
| 16 | +In addition you can specify if the file should be handled as binary or text mode |
| 17 | +"t" - Text - Default value. Text mode |
| 18 | +"b" - Binary - Binary mode (e.g. images) |
| 19 | +
|
| 20 | +
|
| 21 | +""" |
| 22 | +# 1. To open a file use open() |
| 23 | +file = open("test.txt","rt") # Because "r" for read, and "t" for text are the default values, you do not need to specify them. |
| 24 | +print(file.read()) |
| 25 | +file.close() |
| 26 | +""" |
| 27 | +Q. What is life? |
| 28 | +A. Life is temporary environment to execute certain tasks. |
| 29 | +""" |
| 30 | +# 2. Open with "with" |
| 31 | +""" |
| 32 | +Then you do not have to worry about closing your files, the with statement takes care of that. |
| 33 | +""" |
| 34 | +with open("test.txt","rt") as f: |
| 35 | + print(f.read()) |
| 36 | + |
| 37 | +""" |
| 38 | +Q. What is life? |
| 39 | +A. Life is temporary environment to execute certain tasks. |
| 40 | +""" |
| 41 | +""" |
| 42 | +Note: You should always close your files. In some cases, due to buffering, changes made to a file may not show until you close the file. |
| 43 | +""" |
| 44 | + |
| 45 | +# 3. Read Only Parts of the File |
| 46 | +""" |
| 47 | +
|
| 48 | +By default the read() method returns the whole text, but you can also specify how many characters you want to return: |
| 49 | +""" |
| 50 | +with open("test.txt","rt") as f: |
| 51 | + print(f.read(5)) |
| 52 | + # Reads first 5 characters |
| 53 | +# Q. Wh |
| 54 | + |
| 55 | +# 4. Read lines |
| 56 | +with open("test.txt","rt") as f: |
| 57 | + print(f.readline()) # Reads first line |
| 58 | + |
| 59 | +#By calling readline() two times, you can read the two first lines: |
| 60 | + |
| 61 | +"""Example |
| 62 | +Read two lines of the file: |
| 63 | +""" |
| 64 | + |
| 65 | +with open("test.txt") as f: |
| 66 | + print(f.readline()) |
| 67 | + print(f.readline()) |
| 68 | + |
| 69 | +#By looping through the lines of the file, you can read the whole file, line by line: |
| 70 | + |
| 71 | +"""Example |
| 72 | +Loop through the file line by line:""" |
| 73 | + |
| 74 | +with open("test.txt") as f: |
| 75 | + for x in f: |
| 76 | + print(x) |
| 77 | + |
| 78 | +# Read second line |
| 79 | +with open("test.txt") as f: |
| 80 | + lines = f.readlines() |
| 81 | + if len(lines)>=2: |
| 82 | + print(f"second line:: {lines[1]}") |
| 83 | +# second line:: A. Life is temporary environment to execute certain tasks. |
| 84 | + |
| 85 | +# Read last line |
| 86 | +with open("test.txt") as f: |
| 87 | + lines = f.readlines() |
| 88 | + print(f"last line:: {lines[len(lines)-1]}") |
| 89 | +# second line:: Q. How? |
| 90 | + |
| 91 | +### Write to file |
| 92 | +print("-------------------Write in file------------------") |
| 93 | +""" |
| 94 | +Write to an Existing File |
| 95 | +To write to an existing file, you must add a parameter to the open() function: |
| 96 | +
|
| 97 | +"a" - Append - will append to the end of the file |
| 98 | +
|
| 99 | +"w" - Write - will overwrite any existing content |
| 100 | +""" |
| 101 | +# with open("test.txt","w") as f: |
| 102 | +# f.write("Because life is harsh") |
| 103 | +# #open and read the file after the appending: |
| 104 | +# with open("test.txt") as f: |
| 105 | +# print(f.read()) |
| 106 | + |
| 107 | +# with open("test.txt","a") as f: |
| 108 | +# f.write("A. Because life is harsh.\n") |
| 109 | +# #open and read the file after the appending: |
| 110 | +# with open("test.txt") as f: |
| 111 | +# print(f.read()) |
| 112 | + |
| 113 | + |
| 114 | +print("-------------------Create a new file------------------") |
| 115 | + |
| 116 | +# with open("newfile.txt","x") as f: |
| 117 | +# f.write("Hey new file is created \n") |
| 118 | + |
| 119 | +# Remove a file |
| 120 | +import os |
| 121 | +# os.remove("newfile.txt") |
| 122 | + |
| 123 | +# if os.path.exists("newfile.txt"): |
| 124 | +# os.remove("newfile.txt") |
| 125 | +# else: |
| 126 | +# print("The file does not exist") |
| 127 | + |
| 128 | +print("------------------- Json Handling ------------------") |
| 129 | +""" |
| 130 | +JSON (JavaScript Object Notation) |
| 131 | +
|
| 132 | +- A standard data interchange format supported in Python via the json module. |
| 133 | +- Serialization = converting Python objects → JSON string. |
| 134 | +- Deserialization = converting JSON string → Python object. |
| 135 | +- JSON is widely used for data exchange and is interoperable across languages. |
| 136 | +""" |
| 137 | + |
| 138 | +import json |
| 139 | + |
| 140 | +# To convert a dict to json use dumps |
| 141 | + |
| 142 | +x = { |
| 143 | + "name":"Test", |
| 144 | + "age":20 |
| 145 | +} |
| 146 | +"""The json.dumps() method has parameters to order the keys in the result: |
| 147 | +
|
| 148 | +Example |
| 149 | +Use the sort_keys parameter to specify if the result should be sorted or not:""" |
| 150 | + |
| 151 | +y = json.dumps(x, indent=4, sort_keys=True) |
| 152 | +print(y) |
| 153 | +""" |
| 154 | +{ |
| 155 | + "age": 20, |
| 156 | + "name": "Test" |
| 157 | +} |
| 158 | +""" |
| 159 | + |
| 160 | +# Convert json to dict |
| 161 | +js = json.loads(y) |
| 162 | +print(js) |
0 commit comments