如何使用python(版本2.5)删除特定数量的文件?

时间:2020-11-30 07:08:34

I would like to remove two files from a folder at the conclusion of my script. Do I need to create a function responsible for removing these two specific files? I would like to know in some detail how to use os.remove (if that is what I should use) properly. These two files will always be discarded at the conclusion of my script (which packages a series of files together in a zip format). Thanks in advance.

我想在脚本结束时从文件夹中删除两个文件。我是否需要创建一个负责删除这两个特定文件的函数?我想详细了解如何正确使用os.remove(如果这是我应该使用的)。这两个文件将始终在我的脚本结束时丢弃(它以zip格式将一系列文件打包在一起)。提前致谢。

3 个解决方案

#1


-1  

Just call os.remove("path/to/file"). For example, to remove the file .emacs, call

只需调用os.remove(“path / to / file”)。例如,要删除文件.emacs,请调用

os.remove(".emacs")

The path should be a str that's the pathname of the file. It may be relative or absolute.

路径应该是str,它是文件的路径名。它可能是相对的或绝对的。

#2


4  

It sounds like what you really want is a temp file: http://docs.python.org/library/tempfile.html

听起来你真正想要的是一个临时文件:http://docs.python.org/library/tempfile.html

#3


0  

It is perfectly acceptable to have a 'cleanup()' function that you call at the end of your script, which will call 'os.remove()' on your files.

拥有一个在脚本末尾调用的'cleanup()'函数是完全可以接受的,它将在文件上调用'os.remove()'。

#1


-1  

Just call os.remove("path/to/file"). For example, to remove the file .emacs, call

只需调用os.remove(“path / to / file”)。例如,要删除文件.emacs,请调用

os.remove(".emacs")

The path should be a str that's the pathname of the file. It may be relative or absolute.

路径应该是str,它是文件的路径名。它可能是相对的或绝对的。

#2


4  

It sounds like what you really want is a temp file: http://docs.python.org/library/tempfile.html

听起来你真正想要的是一个临时文件:http://docs.python.org/library/tempfile.html

#3


0  

It is perfectly acceptable to have a 'cleanup()' function that you call at the end of your script, which will call 'os.remove()' on your files.

拥有一个在脚本末尾调用的'cleanup()'函数是完全可以接受的,它将在文件上调用'os.remove()'。