日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python文件不存在时创建文件_python-创建一个文件(如果不存在)

發(fā)布時間:2025/10/17 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python文件不存在时创建文件_python-创建一个文件(如果不存在) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python-創(chuàng)建一個文件(如果不存在)

我需要Python的幫助。 我正在嘗試打開一個文件,如果該文件不存在,則需要創(chuàng)建該文件并將其打開以進(jìn)行寫入。 到目前為止,我有:

#open file for reading

fn = input("Enter file to open: ")

fh = open(fn,'r')

# if file does not exist, create it

if (!fh)

fh = open ( fh, "w")

錯誤消息顯示if(!fh)行上存在問題。我可以像在Perl中一樣使用exist嗎?

8個解決方案

35 votes

如果您不需要原子性,則可以使用os模塊:

import os

if not os.path.exists('/tmp/test'):

os.mknod('/tmp/test')

更新:

正如Cory Klein所提到的,在Mac OS上使用os.mknod()必須具有root權(quán)限,因此,如果您是Mac OS用戶,則可以使用open()代替os.mknod()。

import os

if not os.path.exists('/tmp/test'):

with open('/tmp/test', 'w'): pass

Kron answered 2020-07-23T16:06:46Z

32 votes

好吧,首先,在Python中沒有!運算符,即not。但是open也不會默默地失敗-它將引發(fā)異常。 并且需要適當(dāng)?shù)乜s進(jìn)塊-Python使用空格指示塊包含。

這樣我們得到:

fn = input('Enter file name: ')

try:

file = open(fn, 'r')

except IOError:

file = open(fn, 'w')

Antti Haapala answered 2020-07-23T16:07:11Z

20 votes

'''

w write mode

r read mode

a append mode

w+ create file if it doesn't exist and open it in (over)write mode

[it overwrites the file if it already exists]

r+ open an existing file in read+write mode

a+ create file if it doesn't exist and open it in append mode

'''

例:

file_name = 'my_file.txt'

f = open(file_name, 'a+') # open file in append mode

f.write('python rules')

f.close()

我希望這有幫助。 [僅供參考,請使用python 3.6.2版]

Gajendra D Ambi answered 2020-07-23T16:07:35Z

10 votes

使用input()表示Python 3,最近的Python 3版本已棄用IOError異常(現(xiàn)在它是OSError的別名)。 因此,假設(shè)您使用的是Python 3.3或更高版本:

fn = input('Enter file name: ')

try:

file = open(fn, 'r')

except FileNotFoundError:

file = open(fn, 'w')

cdarke answered 2020-07-23T16:07:55Z

6 votes

我認(rèn)為這應(yīng)該工作:

#open file for reading

fn = input("Enter file to open: ")

try:

fh = open(fn,'r')

except:

# if file does not exist, create it

fh = open(fn,'w')

此外,當(dāng)您要打開的文件為fn時,您錯誤地編寫了fh = open ( fh, "w")

That One Random Scrub answered 2020-07-23T16:08:22Z

1 votes

請注意,每次使用此方法打開文件時,文件中的舊數(shù)據(jù)都會被破壞,無論'w +'還是只是'w'。

import os

with open("file.txt", 'w+') as f:

f.write("file is opened for business")

Clint Hart answered 2020-07-23T16:08:42Z

0 votes

首先讓我提到,您可能不希望創(chuàng)建一個文件對象,該文件對象最終可以打開以進(jìn)行讀取或?qū)懭?#xff0c;這取決于不可復(fù)制的條件。 您需要知道可以使用,讀取或?qū)懭肽男┓椒?#xff0c;這取決于您要對文件對象執(zhí)行的操作。

也就是說,您可以使用try:...例外,按照“一個隨機(jī)擦洗”建議的方式進(jìn)行操作。 實際上,這是建議的方法,根據(jù)python的座右銘“請求寬恕比允許容易”。

但是您也可以輕松地測試是否存在:

import os

# open file for reading

fn = raw_input("Enter file to open: ")

if os.path.exists(fn):

fh = open(fn, "r")

else:

fh = open(fn, "w")

注意:請使用raw_input()而不是input(),因為input()會嘗試執(zhí)行輸入的文本。 如果您不小心要測試文件“導(dǎo)入”,則會收到SyntaxError。

Michael S. answered 2020-07-23T16:09:16Z

-2 votes

fn = input("Enter file to open: ")

try:

fh = open(fn, "r")

except:

fh = open(fn, "w")

成功

mahdi babaee answered 2020-07-23T16:09:36Z

總結(jié)

以上是生活随笔為你收集整理的python文件不存在时创建文件_python-创建一个文件(如果不存在)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。