Answers for "python open create file"

24

python append to file

with open(filename, "a+") as f:
  f.write('Hello World')
Posted by: Guest on March-30-2020
24

create text in python if not exists

#Updated dec 2020
#It will creates Text file in path directory using name of file.txt
import os

path = 'some/path/to/file.txt'
if not os.path.exists(path):
    with open(path, 'w') as f:
    	f.write('Hello, world')
#==== OR ===
mode = 'a' if os.path.exists(path) else 'w'
with open(path, mode) as f:
    f.write('Hello, world!n')
Posted by: Guest on December-12-2020
3

python open and read file with

with open('pagehead.section.htm','r') as f:
    output = f.read()
Posted by: Guest on May-15-2020
0

make a file in python

import os
 
newpath = r'C:Program Filesarbitrary' 
if not os.path.exists(newpath):
    os.makedirs(newpath)
Posted by: Guest on August-08-2021

Python Answers by Framework

Browse Popular Code Answers by Language