Answers for "how to make a new file in python"

1

python function to create file and read file

import os
def file_oprator(filename,type_of_prosess_on_file="w+",text=None):
    """This is a function from which you can create files and read files it takes three inportant inputs one name of file which opration to do on file and text if you are using the create and write function"""
    try:
        
        do = open(filename,type_of_prosess_on_file)
        if type_of_prosess_on_file == "w+" or type_of_prosess_on_file == "w" and os.path.isfile(filename):
            do.write(text)
            return text
        else:
            return do.read()
    except:
        return "an unexpected error occurred"
    do.close()
print(file_oprator("creating file with python function","w+","look i have created a file with a python functionnit was as simple as eating a cakenpython is very inportant for the feature"))
Posted by: Guest on October-05-2021
2

python write to file

# using 'with' block

with open("xyz.txt", "w") as file: # xyz.txt is filename, w means write format
  file.write("xyz") # write text xyz in the file
  
# maunal opening and closing

f= open("xyz.txt", "w")
f.write("hello")
f.close()

# Hope you had a nice little IO lesson
Posted by: Guest on November-02-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

Code answers related to "how to make a new file in python"

Python Answers by Framework

Browse Popular Code Answers by Language