how to open files python code example
Example 1: python read file
with open("file.txt", "r") as txt_file:
return txt_file.readlines()
Example 2: open text file in python
f=open("Diabetes.txt",'r')
f.read()
Example 3: python open file
with open('filename', 'a') as f:
f.write(var1)
f.write('data')
f.close()
with open('filename', 'r') as f:
with open('filename', 'x') as f:
with open('filename', 't') as f:
with open('filename', 'b') as f:
with open('filename', 'w') as f:
with open('filename', '+') as f:
Example 4: how to open file explorer in python
import os
import subprocess
FILEBROWSER_PATH = os.path.join(os.getenv('WINDIR'), 'explorer.exe')
def explore(path):
path = os.path.normpath(path)
if os.path.isdir(path):
subprocess.run([FILEBROWSER_PATH, path])
elif os.path.isfile(path):
subprocess.run([FILEBROWSER_PATH, '/select,', os.path.normpath(path)])
Example 5: how to open a file in python
f = open("demofile.txt")