Write a String to a File in Python

In Python, you can write a string to a file using the write() method of a file object. The file must be opened in write mode ('w'), append mode ('a'), or exclusive creation mode ('x'). Let’s explore different ways to write a string to a file with examples.


Examples

1. Writing a String to a File Using write() Method

The write() method writes a string to a file. If the file exists, it will overwrite the content; otherwise, a new file will be created.

</>
Copy
# Define the string to write
text = "Hello, this is a sample text."

# Open the file in write mode
with open("example1.txt", "w") as file:
    file.write(text)

print("String written to file successfully.")

Explanation:

  • The variable text stores the string we want to write.
  • The open() function is used to open the file example1.txt in write mode ('w').
  • The write() method of the file object writes the string to the file.
  • The with statement ensures the file is properly closed after writing.

Output:

The file example1.txt is created with the given content.

2. Writing Multiple Strings Using writelines()

The writelines() method writes a list of strings to a file.

</>
Copy
# Define multiple lines as a list
lines = ["First line\n", "Second line\n", "Third line\n"]

# Open the file in write mode
with open("example2.txt", "w") as file:
    file.writelines(lines)

print("Multiple lines written to file successfully.")

Explanation:

  • The variable lines contains a list of strings, each ending with \n to create a new line.
  • The open() function opens example2.txt in write mode ('w').
  • The writelines() method writes all lines from the list to the file.

Output:

The file example2.txt contains:

3. Appending a String to a File

The append mode ('a') allows adding content to an existing file without erasing its contents.

main.py

</>
Copy
# Define the text to append
append_text = "This is an additional line.\n"

# Open the file in append mode
with open("example3.txt", "a") as file:
    file.write(append_text)

print("String appended to file successfully.")

Explanation:

  • The variable append_text contains the string to be added.
  • The file example3.txt is opened in append mode ('a').
  • The write() method adds the string at the end of the existing content.

Output:

If example3.txt had existing content, the new line is added without overwriting it.

4. Writing a String Using print() with File Parameter

The print() function can write to a file by specifying the file parameter.

</>
Copy
# Define the string
message = "Writing to file using print()."

# Open file in write mode and use print()
with open("example4.txt", "w") as file:
    print(message, file=file)

print("String written using print() function.")

Explanation:

  • The variable message contains the string.
  • The file example4.txt is opened in write mode.
  • The print() function writes the string to the file using the file parameter.

Output:

The file example4.txt contains:

Conclusion

In this tutorial, we covered different ways to write a string to a file in Python:

  1. write(): Writes a single string.
  2. writelines(): Writes multiple strings.
  3. Appending Mode ('a'): Adds content without overwriting.
  4. print() with File Parameter: Uses print function for writing.