WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Python Β· Basic Β· question 13 of 100

How do you read and write files in Python? Explain the different modes.?

πŸ“• Buy this interview preparation book: 100 Python questions & answers β€” PDF + EPUB for $5

In Python, you can read and write files using the built-in open() function. The open() function returns a file object that you can use to interact with the file. Here is the basic syntax for opening a file in Python:

    file = open(filename, mode)

In this syntax, filename is the name of the file that you want to open, and mode is a string that specifies how you want to interact with the file. The mode argument is optional, and if you don’t specify it, the file will be opened in read-only mode by default.

Here are the different modes that you can use with the open() function:

r: Read mode. This is the default mode. If you open a file in read mode, you can read the contents of the file, but you cannot modify it.

w: Write mode. If you open a file in write mode, you can modify the contents of the file. If the file does not exist, it will be created. If the file already exists, its contents will be truncated (i.e., deleted) before you start writing to it.

a: Append mode. If you open a file in append mode, you can add new data to the end of the file. If the file does not exist, it will be created.

x: Exclusive creation mode. If you open a file in exclusive creation mode, the file will be created, but if it already exists, an error will be raised.

b: Binary mode. If you open a file in binary mode, you can read or write binary data (e.g., images, audio files) to the file.

t: Text mode. This is the default mode. If you open a file in text mode, you can read or write text data to the file.

Here is an example of how to open a file in write mode and write some data to it:

    file = open('example.txt', 'w')
    file.write('Hello, world!')
    file.close()

In this example, we open a file called example.txt in write mode, write the string ’Hello, world!’ to the file using the write() method, and then close the file using the close() method.

Here is an example of how to open a file in read mode and read its contents:

    file = open('example.txt', 'r')
    contents = file.read()
    print(contents)
    file.close()

In this example, we open the example.txt file in read mode, read its contents using the read() method, and then print the contents to the console. Finally, we close the file using the close() method.

In summary, the open() function in Python is a powerful tool for reading and writing files. By using different modes with the open() function, you can specify how you want to interact with the file and perform a wide variety of file I/O operations.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview β€” then scores it.
πŸ“ž Practice Python β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Python questions & answers β€” PDF + EPUB for $5

All 100 Python questions Β· All topics