Convert string to file python

How do I create a file-like object (same duck type as File) with the contents of a string?

Convert string to file python

Asclepius

52.1k15 gold badges150 silver badges131 bronze badges

asked Sep 26, 2008 at 19:33

Daryl SpitzerDaryl Spitzer

137k75 gold badges154 silver badges173 bronze badges

For Python 2.x, use the StringIO module. For example:

>>> from cStringIO import StringIO
>>> f = StringIO('foo')
>>> f.read()
'foo'

I use cStringIO (which is faster), but note that it doesn't accept Unicode strings that cannot be encoded as plain ASCII strings. (You can switch to StringIO by changing "from cStringIO" to "from StringIO".)

For Python 3.x, use the io module.

f = io.StringIO('foo')

Convert string to file python

Asclepius

52.1k15 gold badges150 silver badges131 bronze badges

answered Sep 26, 2008 at 19:34

Daryl SpitzerDaryl Spitzer

137k75 gold badges154 silver badges173 bronze badges

3

In Python 3.0:

import io

with io.StringIO() as f:
    f.write('abcdef')
    print('gh', file=f)
    f.seek(0)
    print(f.read())

The output is:

'abcdefgh'

Samuel

7,2878 gold badges42 silver badges39 bronze badges

answered Sep 26, 2008 at 22:00

1

This works for Python2.7 and Python3.x:

io.StringIO(u'foo')

answered Feb 27, 2018 at 15:02

Convert string to file python

guettliguettli

23.9k66 gold badges308 silver badges582 bronze badges

If your file-like object is expected to contain bytes, the string should first be encoded as bytes, and then a BytesIO object can be used instead. In Python 3:

from io import BytesIO

string_repr_of_file = 'header\n byline\n body\n body\n end'
function_that_expects_bytes(BytesIO(bytes(string_repr_of_file,encoding='utf-8')))

answered Feb 5, 2020 at 20:01

Convert string to file python

lensonplensonp

3333 silver badges8 bronze badges

Contents

  • Introduction
  • Example 1: Write String to Text File
  • Example 2: Write String to Text File in Text Mode
  • Writing other than String to Text File
  • Summary

Now you can save or write string to text a file in persistent data storage using Python.

To write string to a Text File, follow these sequence of steps:

  1. Open file in write mode using open() function.
  2. Write string to the file using write() method.
  3. Close the file using close() method.

Convert string to file python

Example 1: Write String to Text File

In the following example, we will take a string constant and write the string to a text file by following the above said sequence of steps.

Python Program

text_file = open("sample.txt", "w")
n = text_file.write('Welcome to pythonexamples.org')
text_file.close()

Output

29

The write() method returns the number of characters written to the text file.

Note that this kind of writing to text file, overwrites the data, if the file is already present. If the file is not present, it creates a new file and then writes the string to the file.

Example 2: Write String to Text File in Text Mode

A file can be opened in two modes: the first one is text mode and the second one is binary mode. By default a file is opened in text mode. However, you can explicitly specify the mode.

In the following example, we will open the file in text mode by appending “t” to the mode and write the string to a text file by following the sequence of steps mentioned at the start of this tutorial.

Python Program

text_file = open("sample.txt", "wt")
n = text_file.write('Welcome to pythonexamples.org')
text_file.close()

Output

29

Writing other than String to Text File

If you would like to write any Python object other than string or a bytes object to a file, using write() method, you should first convert that Python object to a string or bytes object.

Summary

In this tutorial of Python Examples, we learned how to write a string to a text file, with the help of example programs.

How do I convert a string to a CSV file in Python?

Steps to Convert a Text File to CSV using Python.
Step 1: Install the Pandas package. If you haven't already done so, install the Pandas package. ... .
Step 2: Capture the path where your text file is stored. ... .
Step 3: Specify the path where the new CSV file will be saved. ... .
Step 4: Convert the text file to CSV using Python..

How do I convert a string to a file type in Python?

Now you can save or write string to text a file in persistent data storage using Python..
Open file in write mode using open() function..
Write string to the file using write() method..
Close the file using close() method..

Can Python create a text file?

Python provides inbuilt functions for creating, writing, and reading files.

What does Rstrip () do in Python?

Python String rstrip() Method The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.