使用python创建新文本文件时出错?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18533621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error when creating a new text file with python?
提问by Bython
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()
采纳答案by falsetru
If the file does not exists, open(name,'r+')
will fail.
如果文件不存在,open(name,'r+')
将失败。
You can use open(name, 'w')
, which creates the file if the file does not exist, but it will truncate the existing file.
您可以使用open(name, 'w')
, 如果文件不存在,它会创建文件,但它会截断现有文件。
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.
或者,您可以使用open(name, 'a')
; 如果文件不存在,这将创建文件,但不会截断现有文件。
回答by user3725107
You can use open(name, 'a')
您可以使用 open(name, 'a')
However, when you enter filename, use inverted commas on both sides, otherwise ".txt"
cannot be added to filename
但是输入filename的时候,两边都要用引号,否则".txt"
不能加到filename
回答by Ivan
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')
回答by SriSree
instead of using try-except blocks, you could use, if else
而不是使用 try-except 块,你可以使用, if else
this will not execute if the file is non-existent, open(name,'r+')
如果文件不存在,这将不会执行,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
'w' 如果文件不存在则创建一个文件
回答by mai_way
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 :)
这将工作承诺:)
回答by mai_way
You can os.system function for simplicity :
为简单起见,您可以使用 os.system 函数:
import os
os.system("touch filename.extension")
This invokes system terminal to accomplish the task.
这将调用系统终端来完成任务。
回答by prathima
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()