Why are file operations required in Python?
Working with files is a must when dealing with large datasets in machine learning problems. Because Python is a popular data science language, you should be familiar with the various file operations that Python provides.
The readline() function reads a line from a document. The string returned has a trailing newline (“\n”) at the end of it. The size option can also be used to obtain the desired line length. The complete line will be returned by default if the size parameter is not specified.
Syntax:
file.readline(size)
Example:
We have a file named File1 as:
Hello - first line
Second line
Third line
We can print all the lines one by one,
for line in File1:
print (line)
Note: The readlines() function is one that Python offers that is used to read lines. The list of lines up until the end of the file (EOF) is returned.
A single string can be written to a file using the write() function. For example,
file = open('File1.txt','w')
file.write("This is the write command")
file.write("coding is fun")
file.close()
On reading this file, we get the following output:
This is the write command
coding is fun
Write() cannot be used to write a list or tuple’s contents. The writelines() is used in such circumstances. The close() command terminates all the resources in use and frees the system of this particular program.
The file pointer’s current location within the file is returned by the tell() function.
The seek() method can be used to shift the file pointer to a different location.
Syntax:
This method takes two arguments:
f.seek(offset, fromwhere)
offset
represents how many bytes to movefromwhere
, represents the position from where the bytes are moving.Example: seek()
File1 = open('file1.txt','r+')
File1.seek(5)
File1.read()
Output:
is the write command
coding is fun
Note: the seek()
function skipped the first 5 bytes (i.e., ‘This ‘ and read the next bytes till the end of the line.
Example: tell()
The tell()
function can be used to determine the pointer’s current location. It will indicate that the byte’s pointer away from the start.
Syntax:
File1.tell()
Select the file to copy, then make an object from it. Make a new object, then use the open() function to make a new file (writing the path in the open() function makes the file if it doesn’t already exist). read() content from first file. write() the same in the other file.
file1 = open("file1.txt", "r")
file2 = open("file2.txt", "w")
l = file1.readline()
while l:
file2.write(l)
l = file1.readline()
file1.close()
file2.close()
You might view the copied text by opening the file duplicate.txt.
Note: also read about File Handling in Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…