Mohammad Hosein Bin Mohammad Hosein Bin - 1 year ago 41
Python Question

Python script makes a file but doesn't write anything

First, code gets a name and makes a file with

permission (also tested
) and it should write any other input in file, but it doesn't. I get an empty file.

user_name_in =input("gets Input")
fname = user_name_in
f = input()
ufile = open(fname,"w")
while True:

Answer Source

As i wrote in the comments, always use the with block to handle files as it takes care of intricacies you don't have to worry about. Now on the code, you repeat yourself, for example the first two lines are actually one.. This is what it would look when cleaned a bit.

fname = input("gets Input")
with open(fname, "w") as ufile:
    f = input('write something')

And as others also noticed, the answer is never declared, there is no termination condition and the input prompts are either not the best or totally absent.