0

I have list like this:

test_list = ['test1','test2','test3']

I want to create from each element of the array a separate txt file and write content to that like in below:

for test in test_list:
    print (test, outfile=testdir/test + '.txt')

Obviously this code doesn't work. I would like to know how can I do this?

martineau
  • 119,623
  • 25
  • 170
  • 301
Qaqa Leveo
  • 127
  • 1
  • 1
  • 6
  • 2
    `outfile` (which should be `file`) must be a Python's _file object_. __Please read the docs before asking questions and even attempting to write code.__ Quote from `help(print)`: "file: a _file-like object_ (stream); defaults to the current sys.stdout" – ForceBru Dec 12 '17 at 17:49
  • @ForceBru. I know. just wanted to show that what I need :). But I don't know how to create this – Qaqa Leveo Dec 12 '17 at 17:51
  • You don't know how to open files? – ForceBru Dec 12 '17 at 17:52
  • I don't know how to open multiple file. Because this was just example it can be 10000 files – Qaqa Leveo Dec 12 '17 at 17:53
  • Are you sure that you really want to keep 10000 files opened *at the same time*??? – Serge Ballesta Dec 12 '17 at 17:58
  • Possible duplicate of [Directing print output to a .txt file in Python 3](https://stackoverflow.com/questions/36571560/directing-print-output-to-a-txt-file-in-python-3) – mkrieger1 Dec 12 '17 at 17:59

3 Answers3

1
test_list = ['test1','test2','test3']
for test in test_list:
   with open('{}.txt'.format(test),'w+') as f:
      f.write(test)
user2614596
  • 630
  • 2
  • 11
  • 30
  • This is the correct method for Python 3.x as per the docs: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files – dijksterhuis Dec 12 '17 at 18:04
  • 1
    @ForceBru it will if you change `wb` to `w+`. I was talking about the use of `with open` in terms of correct methods. – dijksterhuis Dec 12 '17 at 18:15
0

This will help you

test_list = ['test1','test2','test3']

for test in test_list:
    f = open('{}.txt'.format(test),'wb') #
    f.write(test)
    f.close()

for more visit : https://www.tutorialspoint.com/python/python_files_io.htm

babygame0ver
  • 447
  • 4
  • 16
-1

I thing it will help you.

test_list = ['test1','test2','test3']
for l in test_list:
    with open('{}.txt'.format(l), 'a') as s:
         s.write(l)

If you want to write something you can use same as above.

sanjusci
  • 187
  • 1
  • 9
  • Doesn't work as `s.write` returns an _integer_: `AttributeError: 'int' object has no attribute 'close'`. – ForceBru Dec 12 '17 at 18:15
  • If you are using the `with open` syntax, you shouldn't need to call the `close()` method afterwards. See @user2614596 answer above – dijksterhuis Dec 12 '17 at 18:18
  • Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> test_list = ['test1','test2','test3'] >>> for l in test_list: ... with open('{}.txt'.format(l), 'a') as s: ... s.write(l) ... >>> >>> s >>> not given any error please check once on version 2.7 V – sanjusci Jan 25 '18 at 06:55