-1

I need your help python code to create new file (blank) just name from a list.

my_list = ['jordan', 'bella', 'anabelle']

we want to create

  1. jordan.html
  2. bella.html
  3. anabelle.html

thank you so much everyone

mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • 1
    Does this answer your question? [Create empty file using python](https://stackoverflow.com/questions/12654772/create-empty-file-using-python) – mosc9575 Jan 14 '21 at 11:03

3 Answers3

1

(Edited: for importing and iterating) if you want to iterate through a list:

with open('a.txt') as f:
    a = f.read().split(' ')
for i in a:
    with open(i + '.html', 'w') as f:
        pass
korrupt
  • 11
  • 2
  • thanks for your reply but i want create new file in directory input name files from a.txt list – Thomas Ponco Jan 14 '21 at 10:55
  • You should never use os.popen with an unescaped argument parameter just thrown in like that to do file operations (or anything else unescaped, really). This is unsafe, and one of the worst ways to do it. E.g. open() with mode "w" and closing with no content does the job without these caveats. – E. T. Jan 14 '21 at 11:50
  • True, I'm still learning myself. – korrupt Jan 14 '21 at 11:59
  • 1
    What would you recommend? With open(i) as f? – korrupt Jan 14 '21 at 12:00
  • Yes, open() works fine. That is a built-in python function and doesn't use the system shell, so it doesn't have any of the escaping caveats that os.popen() or os.system() do. Shells in general are a programming language of their own, and it is better not to use them with untrusted input unless you really need to. – E. T. Jan 14 '21 at 12:02
  • 1
    Well thanks for the tip!! I'll edit my answer. – korrupt Jan 14 '21 at 12:09
1
with open("names.txt") as f:
    my_list = f.readline().split()
    for item in my_list:
        with open(item+'.html', 'w') as fp:
            pass
mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • Maybe you'd want to incorporate `with open("names.txt") as f` with `f.readline().split()` from one of the other responses instead of the hardcoded list contents? That might make for a nice, complete response – E. T. Jan 14 '21 at 12:05
  • 1
    Good comment. Thank's. – mosc9575 Jan 14 '21 at 12:07
-1
import os


class IO:

    def __init__(self):
        self.data = open("names.txt").readline().split()

    def show(self):
        return self.data

    def create_files(self):
        for i in self.data:
            os.system("touch " + str(i) + str(".html"))


io = IO()

print(io.create_files())

Try this....and in names.txt just put blank space between names

  • Very unsafe, don't combine shell commands with unescaped user arguments like that, ever. This allows arbitrary code execution with malicious file name inputs. – E. T. Jan 14 '21 at 11:53
  • yea, but its for a newbie who wont deploy it and it is just for practice "proof of work" written under one minute.. I understand what you say and I agree, but context here allow it – TheOriginalOdis Jan 14 '21 at 11:55
  • I would suggest it's not a good idea to teach anyone, including newcomers, such bad practices when this is so incredibly insecure and the correct safe approach (see other response https://stackoverflow.com/a/65717764/1204141 ) is comparatively simple to do. Please don't make anyone use shell commands without escaping. – E. T. Jan 14 '21 at 12:01