Error when creating a new text file with python?
270,130
Solution 1
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
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
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
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
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 :)
Author by
Bython
Updated on July 05, 2022Comments
-
Bython almost 2 years
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()
-
mc110 almost 10 yearsit looks like the previous answer already mentioned open(name, 'a'), so it would have been better to just add your last line as a comment
-
Tom Zych almost 10 years"Inverted commas"? Do you mean single quotes? Python doesn't care whether you enclose a string in single quotes or double quotes. It only matters if the string includes a matching delimiter; enclosing it in the other kind can save having to escape the enclosed character.
-
user over 8 yearsDoes this add anything that didn't already exist in the 2 year old answers above?
-
falsetru over 8 yearsThe question is marked as
python-3.x
in whichraw_input
is not available. -
KI4JGT over 8 yearsNeither "w" or "a" creates a new file for me.
-
KI4JGT over 8 yearsStupid me didn't add the directory Desktop in my path, so I was sitting there missing a part of the file path. . .
-
jfs over 8 yearsto avoid modifying an existing file,
'x'
could be used instead of'w'
,'a'
. -
Adrian Krupa over 8 yearsTag
python-3.x
was added after this answer -
Musixauce3000 about 8 yearsHe changed
name=input()
toname=raw_input()
. Of course, that's deprecated. -
McLan almost 8 yearsThanks for your answer but sadly doesn't work for me as "error occurred"!!
-
Desprit over 7 yearsNot following PEP. Using different indentation. Incorrectly handling exception.