#6 Fun Python — File Operation that look like Database

Python programming language, apart from being used for coding, it can also be used to handle files. Files that are usually handled have a .txt extension.

To get started, you can start by opening the file first. Disini saya akan menyiapkan file.txt bernama myfile yang disimpan di d: di dalamnya akan saya tulis sebuah kata berupa “testing”.

Example

file = open("d:/myfile.txt")
read_file = file.read()
print(read_file)

Output:

  • If in d: there is no file named “myfile.txt”, it will display an error message
--------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-3-9660184a7868> in <module>
----> 1 file = open("d:/myfile.txt")
2 read_file = file.read()
3 print(read_file)

FileNotFoundError: [Errno 2] No such file or directory: 'd:/myfile.txt'
  • But if in d: there is a file named “myfile.txt” then the result
Testing

The example program above, is actually still not good, because if the file is only opened() by Python, it can trigger damage to the file it calls. Anything that starts with open(), must end with close().
Example

# Open file with path file
file = open("d:/myfile.txt")
# To read the text in the file
read_file = file.read()
print(read_file)
# For close the file
file.close()

Output:

Testing

However, if you’re afraid you’ll forget to close the file, you can use another method, such as using with().

with open("d:/myfile.txt", "r") as file:
read = file.read()
print(read)

Output:

Testing

By using with we don’t need to use the close() method anymore, because it’s automatically done by python.

Python can manipulate it with various argument modes. Each mode has different characteristics and uses. Here are the argument modes in Python:

Table

                  | r   r+   w   w+   a   a+
------------------|--------------------------
read | + + + +
write | + + + + +
write after seek | + + +
create | + + + +
truncate | + +
position at start | + + + +
position at end | + +

1. “r”

Read — Just for reading, and its position at the beginning.

Example

# Open file with path file
file = open("d:/data.txt", "r")
# To read the text in the file
read_file = file.read()
print(read_file)
# For close the file
file.close()

Output:

10
0
a
20
30
40
b
80

2. r+

Read + — Can be used for reading and writing. The position is at the beginning.

Example

# Open file with path file
file = open("d:/file1.txt", "r+")
# To read the text in the file
read_file = file.read()
print(read_file)
# For close the file
file.close()

Output:

100
102
99
89
192
938
107
241

3. w

Write — useful for writing, the file if it does not exist, and the contents of the file if the file already exists. The position at the beginning.

Example

file = open("d:/MyFileAgain.txt", "w")
file.write("Hi, my name is Hanizar")
file.close()

Output:

4. w+

Write+ — useful for reading, and writing files. If the file does not exist, it will be created automatically, but if the file already exists, the contents of the file will be truncated. The position at the beginning.

Example

# Open or create file with path file
file = open("d:/myfile.txt", "w+")
# Write text into file
file.write("I Love Python")
# For close the file
file.close()

Output:

5. a

Append — useful for writing. If the file does not exist, it will be created automatically. The position at the end.

Example

# Open or create file with path file
file = open('d:/myfile.txt', "a")
# Write text into file
file.write("You Love Python?")
# Close the file
file.close()

Output:

Because the mode argument a cannot truncate the contents of the text in the file, nor is it positioned at the end. Then the output obtained, as in the picture above.

6. a+

Append+ — useful for reading and writing. If the file does not exist, it will be created automatically. Theposition at the end.

Example

file = open('d:/myfile.txt', "a+")# Write text into file
file.write("Yes, I love it")
# Read and Print text from file
read = file.read()
print(read)
# For close the file
file.close()

Output:

1. In run programs

# EMPTY

2. In file

There is a difference when it is run with mode a and a+, in the result file, it turns out that the text previously entered by mode a, has been replaced/cut by text with mode a+. Both are the same starting from the final position.Even in the program, even though this a+ mode can read files, why does it not appear when ordered to print/read text in the file?Because a+ is at the end, therefore, it will print from where the pointer is, for that, you can use seek(), to set the pointer position.

Example to print text in file

# Open file
file = open('d:/myfile.txt', "a+")
# Write text into file
file.write("Yes, I love it")
# Sets the pointer, starting at 0
file.seek(0)
# Read and Print text from file
read = file.read()
print(read)
# For close the file
file.close()

Output:

  1. In program
I Love PythonYes, I love it
  1. In file

See! The pointer position is now at the beginning

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store