Windows 中的 os.remove() 给出“[错误 32] 被另一个进程使用"; [英] os.remove() in windows gives "[Error 32] being used by another process"
问题描述
我知道这个问题在 SO 和其他地方也被问到了很多.我还是没能完成.如果我的英语不好,我很抱歉
I know this question has been asked before quiet a lot on SO and elsewhere too. I still couldn't get it done. And im sorry if my English is bad
在 linux 中删除文件要简单得多.只是 os.remove(my_file)
完成了这项工作,但在 Windows 中它给出了
Removing file in linux was much more simpler. Just os.remove(my_file)
did the job, But in windows it gives
os.remove(my_file)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: (file-name)
我的代码:
line_count = open(my_file, mode='r') #
t_lines = len(line_count.readlines()) # For total no of lines
outfile = open(dec_file, mode='w')
with open(my_file, mode='r') as contents:
p_line = 1
line_infile = contents.readline()[4:]
while line_infile:
dec_of_line = baseconvert(line_infile.rstrip(),base16,base10)
if p_line == t_lines:
dec_of_line += str(len(line_infile)).zfill(2)
outfile.write(dec_of_line + "\r\n")
else:
outfile.write(dec_of_line + "\r\n")
p_line += 1
line_infile = contents.readline()[4:]
outfile.close()
os.remove(my_file)
这里的my_file
是一个包含文件完整路径结构的变量.像明智的 dec_file
也包含路径,但是到一个新文件.我试图删除的文件是在 read mode
下使用的文件.需要一些帮助.
Here my_file
is a variable that contains complete path structure of a file. Like wise dec_file
also contains path, but to a new file. And the file im trying to remove is the file that's being used under read mode
. Need some help please.
我的尝试:
- 尝试关闭文件
my_file.close()
.我得到的相应错误是AttributeError: 'str' object has no attribute 'close'
.我知道,当一个文件在read mode
当它结束时它会自动关闭文件.但我还是试了一下 - 也按照 https://stackoverflow.com/a 尝试了
os.close(my_file)
/1470388/3869739.我得到的错误是TypeError: an integer is required
- 或者,我是否因为打开了文件而收到此错误两次(用于计算行数和读取文件内容),..?
- Tried closing the file
my_file.close()
. The corresponding error i got wasAttributeError: 'str' object has no attribute 'close'
. I knew, when a file is inread mode
it automatically closes when it comes to the end of the file. But still i gave it a try - Also tried by
os.close(my_file)
as per https://stackoverflow.com/a/1470388/3869739. i got error asTypeError: an integer is required
- Or, am i getting this error just because i have opened the file twice (for counting the line and to read the file content),..?
推荐答案
Pythonic 读取或写入文件的方式是使用 with
上下文.
Pythonic way of reading from or writing to a file is by using a with
context.
读取文件:
with open("/path/to/file") as f:
contents = f.read()
#Inside the block, the file is still open
# Outside `with` here, f.close() is automatically called.
写:
with open("/path/to/file", "w") as f:
print>>f, "Goodbye world"
# Outside `with` here, f.close() is automatically called.
<小时>
现在,如果没有其他进程读取或写入文件,并且假设您拥有所有权限,您应该能够关闭文件.很有可能存在资源泄漏(文件句柄未关闭),因此 Windows 将不允许您删除文件.解决方案是使用with
.
进一步澄清其他几点:
- 它是垃圾收集器,当对象被销毁时会导致流关闭.完成读取后,文件不会自动关闭.如果程序员想倒带,那没有意义,不是吗?
- os.close(..) 在内部调用 C-API
close(..)
,它接受一个整数文件描述符.不是你通过的字符串.
- Its the garbage collector that causes the closure of the stream when the object is destroyed. The file is not auto-closed upon complete reading. That wouldn't make sense if the programmer wanted to rewind, would it?
- os.close(..) internally calls the C-API
close(..)
that takes an integer file descriptor. Not string as you passed.
这篇关于Windows 中的 os.remove() 给出“[错误 32] 被另一个进程使用";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!