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
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)
my code :
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)
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 try's :
- 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),..?