Files Built in Methods

 


File Built In Methods

  • close() : Closes the file.

#program

file = open("my_file_1.txt", "r")
print(file.read())

file.close()

print("\nfile closed")

 #output

Techno Xpresss.
line-
2
line-3
line-4
line-5

file closed


    • if try to access file once closed.

#program

file = open("my_file_1.txt", "r")
data = file.read()

print(data)
file.close()
print("\nfile closed")

data.file.read()
#trying to read file once closed

 #output

Techno Xpresss.
line-
2
line-3
line-4
line-5

file closed
Traceback (most recent call last):
File
"C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 58, in <module>
data.file.read()
AttributeError:
'str' object has no attribute 'file'



  • detach() : Returns the separated raw stream from the buffer.


  • fileno() : Returns a number that represents the stream, from the operating system's perspective.


  • flush() : Flushes the internal buffer.

#program

file = open("my_file_1.txt", "r")
data = file.read()
print(data)

file.flush()
print("After Flushing")
data = file.read()
print(data)
file.close()
 #output

Techno Xpresss.
line-
2
line-3
line-4
line-5

After Flushing



  • isatty() : Returns whether the file stream is interactive or not.



  • read() : Returns the file content.

#program

file = open("my_file_1.txt", "r")

data = file.read()
print(data)
file.close()

 #output

Techno Xpresss.
line-
2
line-3
line-4
line-5



  • readable() : Returns whether the file stream can be read or not.

#program

file = open("my_file_1.txt", "r")
readable = file.readable()
print("my_file_1.txt is readable : ", readable)
file.close()

 #output

my_file_1.txt is readable : True



  • readline() : Returns one line from the file.
    • it will read one line from the time 

#program

file = open("my_file_1.txt", "r")
data = file.readline()
print(data)
data = file.readline()
print(data)
data = file.readline()
print(data)
file.close()

 #output

Techno Xpresss.

line-
2

line-3



  • readlines(): Returns a list of lines from the file.

#program

file = open("my_file_1.txt", "r")
data = file.readlines()
print(data)

for x in data :
print(x)

file.close()

 #output

['Techno Xpresss.\n', 'line-2\n', 'line-3\n', 'line-4\n', 'line-5\n']
Techno Xpresss.

line-
2

line-3

line-4

line-5



  • seek() : Change the file position.

#program

file = open("my_file_1.txt", "r")
data = file.read()
print(data)

file.seek(
5)
data = file.read()
print(data)

file.seek(
12)
data = file.read()
print(data)

file.close()

 #output

Hi, this is Techno Xpresss.

his
is Techno Xpresss.

Techno Xpresss.



  • seekable() : Returns whether the file allows us to change the file position.

#program

file = open("my_file_1.txt", "r")
data = file.read()
print(data)

seekable = file.seekable()
print("my_file_1.txt is seekable : ", seekable)

file.close()

 #output

Hi, this is Techno Xpresss.

my_file_1.txt
is seekable : True



  • tell() : Returns the current file position.

#program

file = open("my_file_1.txt", "r")
data = file.read()
print(data)

position = file.tell()
print("current position index : ", position)

file.seek(
0)
position = file.tell()
print("current position index : ", position)
file.close()

 #output

Hi, this is Techno Xpresss.

current position index :
29
current position index : 0



  • truncate() : Resizes the file to a specified size.


  • writable() : Returns whether the file can be written to or not.

#program

file = open("my_file_1.txt", "r")
data = file.read()
print(data)

writable = file.writable()
print("cmy_file_1.txt is writable if opened in read mode : ", writable)
file.close()

file =
open("my_file_1.txt", "a")
writable = file.writable()
print("cmy_file_1.txt is writable if opened in append mode : ", writable)
file.close()

 #output

Hi, this is Techno Xpresss.

cmy_file_1.txt
is writable if opened in read mode : False
cmy_file_1.txt is writable if opened in append mode : True



  • write() : Writes the specified string to the file.

#program

file = open("my_file.txt", "r")
file_1 =
open("my_file_1.txt", "w")
data = file.read()
print("data read successfully\n")
print(data)
file_1.write(data)
print("\ndata written successfully")

file.close()

 #output

data read successfully

Hi
, this is Techno Xpresss.
This
is a sample file.

data written successfully



  • writelines(): Writes a list of strings to the file.

#program

file = open("my_file.txt", "r")
file_1 =
open("my_file_1.txt", "w")
data = file.readlines()
print(data)
file_1.writelines(data)

file.close()
file_1.close()

file =
open("my_file_1.txt", "r")
data = file.read()
print(data)

 #output

['Hi, this is Techno Xpresss.\n', 'This is a sample file.\n', 'Hope you all are doing well.']
Hi
, this is Techno Xpresss.
This
is a sample file.
Hope you
all are doing well.






Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India