-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

1

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

2 Comments

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
Good comment. Thank's.
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

6 Comments

thanks for your reply but i want create new file in directory input name files from a.txt list
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.
True, I'm still learning myself.
What would you recommend? With open(i) as f?
Well thanks for the tip!! I'll edit my answer.
|
-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

3 Comments

Very unsafe, don't combine shell commands with unescaped user arguments like that, ever. This allows arbitrary code execution with malicious file name inputs.
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
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 stackoverflow.com/a/65717764/1204141 ) is comparatively simple to do. Please don't make anyone use shell commands without escaping.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.