Answers for "create a file if not exists"

9

python create file if not exists

import os

if not os.path.exists(path):
    with open(path, 'w'):
Posted by: Guest on May-09-2020
0

create a file if it not exesists

import os

file_path = r'E:pynativeaccountprofit.txt'
if os.path.exists(file_path):
    print('file already exists')
else:
    # create a file
    with open(file_path, 'w') as fp:
        # uncomment if you want empty file
        fp.write('This is first line')
Posted by: Guest on August-21-2021

Code answers related to "create a file if not exists"

Python Answers by Framework

Browse Popular Code Answers by Language