user8516396
user8516396

Reputation:

Python automatically closing

Can someone tell me why when I open this python file it automatically closes?

import itertools   

for combination in itertools.product(xrange(10), repeat=4):
    print ''.join(map(str, combination))
    with open("C:\Users\User\Desktop\test.txt", "a") as myfile:
        myfile.write(join(map(str, combination)))

fixed indentation

Upvotes: 0

Views: 11187

Answers (6)

Sapu
Sapu

Reputation: 1

following line adds a prompt asking the user to press Enter before exiting the script. It keeps the script running until a user interacts, allowing you to view the output and outcomes. its a simple method hope it helps.

input("Press Enter to exit...")

Upvotes: 0

Sanket
Sanket

Reputation: 745

For your reference see this article Python 'with' keyword

When we open a file using with keyword, then we need not to close that explicitly. Also this is the best practice to do file handling.

Also fix indentation of last line of your code

Upvotes: 0

Clock Slave
Clock Slave

Reputation: 7967

Thats because you are using the with style of opening files. The file closes when you exit the with block. Its a safe way of opening files. That way you don't have to explicity call the close method on myfile. To avoid this you can use

myfile = open("C:\\Users\\User\\Desktop\\test.txt", "a")
myfile.write(join(map(str, combination)))

Note that once you are done using the file make sure you use myfile.close()

You can go through this page for details

EDIT

Try using this

import itertools
with open(r"C:\Users\User\Desktop\test.txt", "a") as myfile:
    for combination in itertools.product(range(10), repeat=4):
        print (''.join(map(str, combination)))
        myfile.write(''.join(map(str, combination)))

Upvotes: 2

SajidSalim
SajidSalim

Reputation: 359

About 'with' commands: Understanding Python's "with" statement

Short description: They close your file, after the end of the block execution even if you forget to close it yourself. Its a nicer way to put your code, instead of try-finally.

In your case, it opens once for each iteration of the loop. And closes at the end. Then, opens it for the next iteration again. That is inefficient.

Just a suggestion, you want to avoid opening the file inside your for loop. You could call it outside, so that it is opened once, and then you run the loop. The with statement will automatically close the file at the end of the block execution. But again, depends on your context of use.

with open(r"C:\Users\User\Desktop\test.txt", "a") as myfile:
    for combination in itertools.product(xrange(10), repeat=4):
        print ''.join(map(str, combination))
        myfile.write(''.join(map(str, combination)))

Upvotes: 0

wsy
wsy

Reputation: 140

You should also modify the last line into:

myfile.write(''.join(map(str, combination)))

And reference @RetardedJoker's answer

Here is my code:

import itertools

with open("C:\\Users\\User\\Desktop\\test.txt", "a") as myfile:
    for combination in itertools.product(xrange(10), repeat=4):
        result = ''.join(map(str, combination))
        print result
        myfile.write(result)

Upvotes: 0

Sraw
Sraw

Reputation: 20224

import itertools

for combination in itertools.product(xrange(10), repeat=4):
    print ''.join(map(str, combination))
    with open("C:\Users\User\Desktop\test.txt", "a") as myfile:
        myfile.write(join(map(str, combination)))

now it will work. with will create a block, so you need to indent. Keep in mind:)

Upvotes: 0

Related Questions