Python File Write

 # File handling 

# Read from user and write to file


fp = None


try:

fp = open("opt.txt","w")

except IOError as e:

print("File opening error : ",e)

except:

print("Unknown error.")


else:

S = input("Enter data to write to file: ")

fp.write(S)


finally:

if(fp):

fp.close()


"""

Sample output

>python FileWrite.py

Enter data to write to file: Welcome to python programming.


Output file: opt.txt

Welcome to python programming.

"""





No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu