Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pknowledge/9ae8d0621b0932a996f8a36333fe9566 to your computer and use it in GitHub Desktop.
Save pknowledge/9ae8d0621b0932a996f8a36333fe9566 to your computer and use it in GitHub Desktop.
Python Tutorial for Beginners 41 - Create a Text File and Write in It Using Python. https://youtu.be/walXPsausPI
# Example 1 ---------------
fh = open('demo.txt', 'w')
try:
for i in range(10):
fh.write("this is line no %d\n" % (i+1))
finally:
fh.close()
# Example 2 ---------------------
with open('C:\\files\\demo2.txt', 'w+') as fh:
for i in range(10):
fh.write("this is line no %d\n" % (i + 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment