0% found this document useful (0 votes)
11 views3 pages

Cse Material5

The document discusses several common attributes and methods of file objects in Python. When a file is opened using the open function, it returns a file object that has attributes like name, mode, and encoding, as well as methods such as isatty(), truncate(), flush(), fileno() that allow interacting with and modifying the file.

Uploaded by

aasthaa1805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Cse Material5

The document discusses several common attributes and methods of file objects in Python. When a file is opened using the open function, it returns a file object that has attributes like name, mode, and encoding, as well as methods such as isatty(), truncate(), flush(), fileno() that allow interacting with and modifying the file.

Uploaded by

aasthaa1805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

'''In Python, when you open a file using the open function, you get a file object.

This file
object has various attributes and methods that you can use to interact with the file. Here are
some common attributes of a file object:'''

In [1]: #'name: Returns the name of the file.'


file=open('example1.txt')
print(file.name)
file.close()

example1.txt

In [2]: #mode: indicates the mode in which the file was opened
#(e.g., 'r' for read, 'w' for write, 'a' for append).
file=open('example1.txt')
print(file.mode)
file.close()

In [3]: #encoding: represents the encoding of the file (e.g., 'utf-8').


#It specifies how the bytes in the file are to be interpreted as characters.
# Create a string with ASCII and non-ASCII characters
content_to_write = "Hello, 你好, नमस्ते"

# Open a file in write mode with a specific encoding (UTF-8)


with open('example_encoding.txt', 'w', encoding='utf-8') as file:
# Write the content to the file
file.write(content_to_write)

# Open the file in read mode with the same encoding


with open('example_encoding.txt', 'r', encoding='utf-8') as file:
# Read and print the content of the file
content = file.read()
print(content)

Hello, 你好, नमस्ते

In [4]: #isatty() method:


'''It is a method available for file objects in Python.
It is used to check whether the file is associated with a terminal device or not.
The method returns True if the file is connected to a terminal (tty or console),
and False otherwise.'''
##############################################
# Open a file in read mode
with open('example_file.txt', 'r') as file:
# Check if the file is associated with a terminal
if file.isatty():
print("The file is connected to a terminal.")
else:
print("The file is not connected to a terminal.")

The file is not connected to a terminal.

In [5]: #truncate() method:


'''It is a built-in method in Python that is used with
file objects to resize the file to a specified size.'''
#######################
# Open a file in read-and-write mode
with open('example_write.txt', 'r+') as file:
# Read the content of the file
content = file.read()
print("Original Content:\n", content)

# Truncate the file to the first 10 characters


file.truncate(10)

# Move the file pointer to the beginning


file.seek(0)

# Read the modified content


modified_content = file.read()
print("\nModified Content:\n", modified_content)

Original Content:

This is another line.


Yet another line.
New line added in r+ mode.
Adding another line.
____This line is appended in a+ mode.
Adding another line.

Modified Content:

This is

In [6]: #flush() method:


'''It's used to clear the internal buffer associated with the file.
When you write data to a file, the operating system often buffers the data in
memory before actually writing it to the file on disk.
The flush() method forces the contents of the buffer to be written to the file.'''
########################
# Open a file in write mode
with open('example_wflush.txt', 'w') as file:
# Write some data to the file
file.write('Hello, World!')

# Flush the buffer to ensure data is written to the file immediately


file.flush()
############################
with open('example_write.txt', 'r') as file:
# Write some data to the file
data= file.read()
print(data)

This is

In [7]: #fileno() method:


'''It returns the file descriptor associated with the file object.'''
#############################
# Open a file in read mode
with open('example.txt', 'r') as file:
# Get the file descriptor
fd = file.fileno()
print("File Descriptor:", fd)

File Descriptor: 3

In [ ]:

You might also like