7

I've been messing around with lists and creating files from a list. The below works fine but I'm sure that there is a better and cleaner way for doing this. I understand the concept of a loop but can't find a specific example which I could remodel to fit what I am doing. Please could someone please point me in the right direction of looping my items list through the f.write code only the once, to generate the files that I'm after.

    items = [ "one", "two", "three" ]

    f = open (items[0] + " hello_world.txt", "w")
    f.write("This is my first line of code")
    f.write("\nThis is my second line of code with " + items[0] + " the first item in my list")
    f.write ("\nAnd this is my last line of code")

    f = open (items[1] + " hello_world.txt", "w")
    f.write("This is my first line of code")
    f.write("\nThis is my second line of code with " + items[1] + " the first item in my list")
    f.write ("\nAnd this is my last line of code")

    f = open (items[2] + " hello_world.txt", "w")
    f.write("This is my first line of code")
    f.write("\nThis is my second line of code with " + items[2] + " the first item in my list")
    f.write ("\nAnd this is my last line of code")
    f.close()

3 Answers 3

16

You can use a for loop and a with statement like this. The advantage of using with statement is that, you dont have to explicitly close the files or worry about the cases where there is an exception.

items = ["one", "two", "three"]

for item in items:
    with open("{}hello_world.txt".format(item), "w") as f:
        f.write("This is my first line of code")
        f.write("\nThis is my second line of code with {} the first item in my list".format(item))
        f.write("\nAnd this is my last line of code")
Sign up to request clarification or add additional context in comments.

1 Comment

This works just as great as the above answer but perhaps is a little more friendlier on the kinda code that I want to add. Cheers
2

Regular for loop - with some optimizing.

Data:

items = ["one", "two", "three" ]
content = "This is the first line of code\nThis is my second line of code with %s the first item in my list\nAnd this is my last line of code"

Loop:

for item in items:
    with open("%s_hello_world.txt" % item, "w") as f:
        f.write(content % item)

1 Comment

This worked great, thank you very much. I guessed that there would be a for statement in there somewhere but it just a matter of knowing where to put it. Cheers again
2

You should use a for loop

for item in  [ "one", "two", "three" ]:
    f = open (item + " hello_world.txt", "w")
    f.write("This is my first line of code")
    f.write("\nThis is my second line of code with " + item  + " the first item in my list")
    f.write ("\nAnd this is my last line of code")
    f.close()

1 Comment

@geomiles The for statement line should have a : at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.