Skip to content

Instantly share code, notes, and snippets.

@Elijah-trillionz
Created September 18, 2021 18:31
Show Gist options
  • Save Elijah-trillionz/6ffbb754c2ae6b7e439c941bce850786 to your computer and use it in GitHub Desktop.
Save Elijah-trillionz/6ffbb754c2ae6b7e439c941bce850786 to your computer and use it in GitHub Desktop.
Handling files in php
# TODO: python delete file and folders
import os
# create file
html = open('demo.html', 'x')
html.close()
# write to new file
html = open('demo.html', 'w')
html.write('<html>\n<body>\n<p>\nHello World</p>\n</body>\n</html>')
html.close()
# read
html = open('demo.html')
print(html.read())
# append
html = open('demo.html', 'a')
html.write('</>')
html.close()
# read update file
html = open('demo.html')
print(f'Updated HTML is \n {html.read()}')
html.close()
# check if file exists before deleting
if os.path.exists('demo.html'):
os.remove('demo.html')
print('HTML successfully created, written on, read, appended on, and deleted. Thank you for learning Python')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment