Check sibling questions

Write a program that checks to see if the file name exists, it will give you an error message, if not, it will create the file.

 

Answer:

import os

if os.path.isfile('sample.txt'):

print ("You are trying to create a file that already exist!")

else:

f = open("sample.txt",'w')

Question 8-Write a program - Teachoo.JPG

Introducing your new favourite teacher - Teachoo Black, at only ₹83 per month


Transcript

Question 8 Write a program that checks to see if the file name exists, it will give you an error message, if not, it will create the file. Imports the ‘os module’ Checks if the file already exists Prints error message if the file already exists If the file doesn’t exist, creates a new file Output

CA Maninder Singh's photo - Expert in Practical Accounts, Taxation and Efiling

Made by

CA Maninder Singh

CA Maninder Singh is a Chartered Accountant for the past 12 years. He also provides Accounts Tax GST Training in Delhi, Kerala and online.