Python How to Write to a Text File

Looking to write a string to a text file using Python?

If that's the case, you may use the following template to achieve this goal:

myText = open(r'path where the text file will be created\file name.txt','w') myString = 'Type your string here' myText.write(myString) myText.close()        

In this short guide, you'll see how to:

  • Write a string to a text file
  • Overwrite the original string
  • Display a list of strings in the text file using for loop
  • Deal with integers

Steps to Write a String to a Text File using Python

Step 1: Specify the path for the text file

To begin, specify the path where the text file will be created.

In my case, the path where the text file will be created is: C:\Users\Ron\Desktop\Test

Empty Folder

Step 2: Write a string to a text file using Python

Next, write your string to the text file using this template:

myText = open(r'path where the text file will be created\file name.txt','w') myString = 'Type your string here' myText.write(myString) myText.close()

For our example:

  • The path where the text file will be created is: C:\Users\Ron\Desktop\Test
  • The file name is: my_text_file
  • myString contains the following text: 'This is a Test'

So the full Python code would look as follows (don't forget to place 'r' before your path name to avoid any errors in the path):

myText = open(r'C:\Users\Ron\Desktop\Test\my_text_file.txt','w') myString = 'This is a Test' myText.write(myString) myText.close()        

Once you run the code (adjusted to your path), you'll then see the new text file in your specified location:

How to Write a String to a Text File using Python

Open the text file and you'll see the actual string:

Write to a text file

Overwrite the String

What if you want to overwrite the original string with a new value?

For instance, what if you want to change the string to the following value:

'This is a NEW Test'

In that case, simply edit the text as follows:

myString = 'This is a NEW Test'        

So the new Python code would look like this:

myText = open(r'C:\Users\Ron\Desktop\Test\my_text_file.txt','w') myString = 'This is a NEW Test' myText.write(myString) myText.close()        

Run the code, and you'll see the new string:

String in a text file

List of Strings

Say that you want to display a list of strings in your text file.

Here is an example of a list with strings:

myList = ['This is a Test','This is ALSO a Test', 'This is a FINAL Test']        

In that case, you may use a for loop to display your list of strings in the text file:

myText = open(r'C:\Users\Ron\Desktop\Test\my_text_file.txt','w') myList = ['This is a Test','This is ALSO a Test', 'This is a FINAL Test']  for i in myList:     myText.write(i + '\n')  myText.close()        

You'll now see that each of the strings is presented in a new line (by adding '\n' to the code):

How to Write a String to a Text File using Python

Dealing with Integers

If you try to print integers into the text file, you'll get the following error:

write() argument must be str, not int

You may then choose to convert the integer to a string.

For example, let's suppose that you have two integers (3 and 5), and you want to present the sum of those integers in the text file.

In that case, you may apply the following syntax to achieve the above goal (notice that str() was used to convert the integer to a string):

myText = open(r'C:\Users\Ron\Desktop\Test\my_text_file.txt','w') sumOfValues = 3 + 5 myText.write(str(sumOfValues)) myText.close()        

You'll then get the sum of 8 in the text file:

Sum of integers

Python How to Write to a Text File

Source: https://datatofish.com/string-to-text-file-python/

Related Posts

0 Response to "Python How to Write to a Text File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel