删除文件(如果存在);Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16168018/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:55:53  来源:igfitidea点击:

deleting file if it exists; python

pythonfile-iotry-catch

提问by Yui

I want to create a file; if it already exists I want to delete it and create it anew. I tried doing it like this but it throws a Win32 error. What am I doing wrong?

我想创建一个文件;如果它已经存在,我想删除它并重新创建它。我尝试这样做,但它会引发 Win32 错误。我究竟做错了什么?

try:
    with open(os.path.expanduser('~') + '\Desktop\input.txt'):
        os.remove(os.path.expanduser('~') + '\Desktop\input.txt')
        f1 = open(os.path.expanduser('~') + '\Desktop\input.txt', 'a')
except IOError:
    f1 = open(os.path.expanduser('~') + '\Desktop\input.txt', 'a')

采纳答案by Aya

You're trying to delete an open file, and the docs for os.remove()state...

您正在尝试删除一个打开的文件,并且os.remove()状态文档...

On Windows, attempting to remove a file that is in use causes an exception to be raised

在 Windows 上,尝试删除正在使用的文件会导致引发异常

You could change the code to...

您可以将代码更改为...

filename = os.path.expanduser('~') + '\Desktop\input.txt'
try:
    os.remove(filename)
except OSError:
    pass
f1 = open(filename, 'a')

...or you can replace all that with...

......或者你可以用......替换所有这些

f1 = open(os.path.expanduser('~') + '\Desktop\input.txt', 'w')

...which will truncate the file to zero length before opening.

...这将在打开之前将文件截断为零长度。

回答by RichieHindle

Windows won't let you delete an open file (unless it's opened with unusual sharing options). You'll need to close it before deleting it:

Windows 不会让你删除打开的文件(除非它是用不寻常的共享选项打开的)。您需要在删除它之前关闭它:

try:
    with open(os.path.expanduser('~') + '\Desktop\input.txt') as existing_file:
        existing_file.close()
        os.remove(os.path.expanduser('~') + '\Desktop\input.txt')

回答by ndpu

You can use open with mode parameter = 'w'. If mode is omitted, it defaults to 'r'.

您可以使用带有模式参数 = 'w' 的 open。如果省略 mode,则默认为 'r'。

with open(os.path.expanduser('~') + '\Desktop\input.txt', 'w')

wTruncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

w 将文件截断为零长度或创建用于写入的文本文件。流位于文件的开头。

回答by jamylak

You are trying to remove the file while it is open, you don't even need that withthere to delete it:

您试图在文件打开时with删除它,您甚至不需要在那里删除它:

path = os.path.join(os.path.expanduser('~'), 'Desktop/input.txt')
with open(path, 'w'): as f:
    # do stuff

Deletes if it exists

如果存在则删除

回答by Maryam Arshi

Try this:

尝试这个:

 from os import path, 
    PATH = os.path.expanduser('~') + '\Desktop\input.txt'
    if path.isfile(PATH):
       try:
          os.remove(os.path.expanduser('~') + '\Desktop\input.txt')
       except OSError:
          pass

edited :

编辑:

from os import path, 
        PATH = os.path.expanduser('~') + '\Desktop\input.txt'
        try:
            os.remove(os.path.expanduser('~') + '\Desktop\input.txt')
        except OSError:
            pass