Error when creating a new text file with python?

PythonFilePython 3.xFile Io

Python Problem Overview


This function doesn't work and raises an error. Do I need to change any arguments or parameters?

import sys

def write():
	print('Creating new text file') 
	
	name = input('Enter name of text file: ')+'.txt'  # Name of text file coerced with +.txt
	
	try:
		file = open(name,'r+')   # Trying to create a new file or open one
		file.close()
		
	except:
		print('Something went wrong! Can\'t tell what?')
		sys.exit(0) # quit Python
		
write()

Python Solutions


Solution 1 - Python

If the file does not exists, open(name,'r+') will fail.

You can use open(name, 'w'), which creates the file if the file does not exist, but it will truncate the existing file.

Alternatively, you can use open(name, 'a'); this will create the file if the file does not exist, but will not truncate the existing file.

Solution 2 - Python

instead of using try-except blocks, you could use, if else

this will not execute if the file is non-existent, open(name,'r+')

if os.path.exists('location\filename.txt'):
    print "File exists"

else:
   open("location\filename.txt", 'w')

'w' creates a file if its non-exis

Solution 3 - Python

following script will use to create any kind of file, with user input as extension

import sys
def create():
    print("creating new  file")
    name=raw_input ("enter the name of file:")
    extension=raw_input ("enter extension of file:")
    try:
        name=name+"."+extension
        file=open(name,'a')
        
        file.close()
    except:
            print("error occured")
            sys.exit(0)
       
create()
    

Solution 4 - Python

This works just fine, but instead of

name = input('Enter name of text file: ')+'.txt' 

you should use

name = raw_input('Enter name of text file: ')+'.txt'

along with

open(name,'a') or open(name,'w')

Solution 5 - Python

import sys

def write():
    print('Creating new text file') 

    name = raw_input('Enter name of text file: ')+'.txt'  # Name of text file coerced with +.txt

    try:
        file = open(name,'a')   # Trying to create a new file or open one
        file.close()

    except:
        print('Something went wrong! Can\'t tell what?')
        sys.exit(0) # quit Python

write()

this will work promise :)

Solution 6 - Python

You can os.system function for simplicity :

import os
os.system("touch filename.extension")

This invokes system terminal to accomplish the task.

Solution 7 - Python

You can use open(name, 'a')

However, when you enter filename, use inverted commas on both sides, otherwise ".txt"cannot be added to filename

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBythonView Question on Stackoverflow
Solution 1 - PythonfalsetruView Answer on Stackoverflow
Solution 2 - PythonSriSreeView Answer on Stackoverflow
Solution 3 - PythonprathimaView Answer on Stackoverflow
Solution 4 - PythonIvanView Answer on Stackoverflow
Solution 5 - PythonDaphnneView Answer on Stackoverflow
Solution 6 - Pythonuser5449023View Answer on Stackoverflow
Solution 7 - Pythonuser3725107View Answer on Stackoverflow