<jupyter_start><jupyter_text>**File Handling** > Python supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files.      > Each line of a file is terminated with a special character, called the EOL or End of Line characters like comma ( **,** ) or newline character **\n**. **Topics Covered**       > Open     > Read       > Write     > Close     > Delete     > File Attributes---------- 1. **Open a File**    > The `open()` function takes two parameters; filename, and mode. The available modes are :        ========= ===============================================================    Character Meaning    --------- ---------------------------------------------------------------    'r'       open for reading (default)    'w'       open for writing, truncating the file first    'x'       create a new file and open it for writing    'a'       open for writing, appending to the end of the file if it exists    'b'       binary mode    't'       text mode (default)    '+'       open a disk file for updating (reading and writing)    'U'       universal newline mode (deprecated)    ========= =============================================================== **Note** : "r" for read, and "t" for text are the default values,<jupyter_code>f = open("HelloWorld.txt")<jupyter_output><empty_output><jupyter_text>> Make sure the file exists in current directory, or else you will get `FileNotFoundError`.<jupyter_code>f = open("C:\\HelloWorld.txt")<jupyter_output><empty_output><jupyter_text>> If the file is not in current working directory then we can specify full path **Q**. How to check current working directory?<jupyter_code>import os
os.getcwd()
type(f)<jupyter_output><empty_output><jupyter_text>**Q**. Fetch file from other folder.<jupyter_code>f = open(r"F:\\PythonTraining\HelloWorld.txt")<jupyter_output><empty_output><jupyter_text>-------------2. **Read**<jupyter_code>f.read()<jupyter_output><empty_output><jupyter_text>> By default the `read()` method returns the whole text, but we can also specify how many characters we want to read.       > Returns empty sting `''` if no content found in file or if all content has been read.<jupyter_code>f.read()<jupyter_output><empty_output><jupyter_text>> Specifying number of characters to read.<jupyter_code>f = open("HelloWorld.txt")
f.read(10)<jupyter_output><empty_output><jupyter_text>> To return one line we can use `readline()`<jupyter_code>f = open("HelloWorld.txt")
f.readline()<jupyter_output><empty_output><jupyter_text>> To read each line in the file we can use for loop<jupyter_code>f = open("HelloWorld.txt")
for line in f:
  print(line)<jupyter_output>Welcome to my Github page.

Know all about Python here.<jupyter_text>> Reads and returns each line in the file as a separate element in a list.<jupyter_code>f = open("HelloWorld.txt")
f.readlines()<jupyter_output><empty_output><jupyter_text>> We can specify how many line to be read by providing value for additional parameter `sizehint`.                       > The function, without `sizehint` given, reads a file till it reaches the EOF and then returns an empty string.<jupyter_code>f = open("HelloWorld.txt")
f.readlines(1)<jupyter_output><empty_output><jupyter_text>----------3. **Close** :  > When all operations on  the file have finished we need to close the file.      > Closing a file will free up the resources that were tied with the file. > An exception can occur when we are performing any operations on file. There are chances that code exits without closing the file.      > To avoid such situations, it is always safer to close the file in finally block.<jupyter_code>try:
   f = open("HelloWorld.txt")
   # perform file operations
finally:
   f.close()<jupyter_output><empty_output><jupyter_text>-------4. **Using with** : > The best way to close a file is by using the `with` statement. This ensures that the file is closed when the block inside the `with` statement is exited.       > We don't need to explicitly call the `close()` method. It is done internally.<jupyter_code>with open("HelloWorld.txt") as f:
   # perform file operations
    print(f.readlines(1))

print("File has been closed")<jupyter_output>['Welcome to my Github page.\n']
File has been closed<jupyter_text>-------**NOTE**: If we try to open the file which is closed, we will encounter an error.<jupyter_code>f = open("HelloWorld.txt")
f.close()
f.read()<jupyter_output><empty_output><jupyter_text>5. **Write**: > To write to a existing file, We need to open the file in `'w'` , `'a'` or `'x'` mode.  > An extra care has to be taken if we are using `'w'` mode. As it can overwrite the file.         `"x"` - Create - Create a file, returns an error if the file exist.       `"a"` - Append - Create a file if the specified file does not exist.       `"w"` - Write - Create a file if the specified file does not exist.<jupyter_code># An empty file is created
f = open("Empty.txt", "x")
with open("MyFile.txt",'w') as f:
   f.write("Welcome to Python Classes\n")
   f.write("This is my first file\n\n")
   f.write("And it contains three lines\n")<jupyter_output><empty_output><jupyter_text>> Whem mode is `w`.> The above creates a new file named MyFile.txt in the current directory if it does not exist.       > If it does exist, it will overwritten.------------ > When Mode is `x`.          > If a file exists it will through an error.        > Else it will create a new file.<jupyter_code>with open("MyFile.txt",'x') as f:
   f.write("Welcome to Python Classes\n")
   f.write("This is my first file\n\n")
   f.write("And it contains three lines\n")<jupyter_output><empty_output><jupyter_text>-----> If we try to write when we have opened the file in read mode or vice versa. We will encounter an error.<jupyter_code>f = open("HelloWorld.txt", 'r')
f.write("Trying to write.")
f.close()<jupyter_output><empty_output><jupyter_text>-------> When we want to read and write to a same file. We need to use `+` for mode.<jupyter_code>fileObj = open("HelloWorld.txt", 'r+')
fileObj.close()<jupyter_output><empty_output><jupyter_text>----------6. **File Object Attributes** When a file is opened, A file object is created.      Following informations can be retrived from that object.      * `file.closed` : Returns true if file is closed, false otherwise. * `file.mode` : Returns access mode with which file was opened. * `file.name` : Returns name of the file.<jupyter_code>fileObj = open("HelloWorld.txt")
print("Name of the file: {0}".format(fileObj.name))
print("Closed or not : {0}".format(fileObj.closed))
print("Opening mode : {0}".format(fileObj.mode))

fileObj.close()<jupyter_output>Name of the file: HelloWorld.txt
Closed or not : False
Opening mode : r<jupyter_text>------------ 7. **Delete** :      > To delete a file, we must import the OS module, and run its `os.remove()` function.<jupyter_code>import os
os.remove("HelloWorld.txt")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15070260969314597, 'fraction_numerical': 0.002007456266131345, 'mean_word_length': 3.881035689293212, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 114, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '104554', 'license': 'MIT', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2063, 'n_words': 792, 'path': 'Basics/FileHandling.ipynb', 'repo_name': 'gagana-hg/Python', 'size': '6974'}