Windows OS , Bizarre File Pointer Fact

T

Taygun Kekec

Code :
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os

if os.name == 'nt':
OS_Selection = 0
elif os.name == 'posix':
OS_Selection = 1
else :
OS_Selection = 1

del_cmd_os = ( "del","rm")
filelist = ("ddd.txt","eee.txt","fff.txt")

# Creating Files
for elem in filelist:
open( elem, 'w').write("Selam")

#
# Removing Files
for elem in filelist:
fp = open( elem, 'r')
os.system( "%s %s" % (del_cmd_os[OS_Selection], fp.name))
fp.close()
#

Run this code on Windows XP , it says "the file is being used by
another process" and fails to delete the file. I tried replacing :
#
for elem in filelist:
open( elem, 'w').write("Selam")
#
with :
#
for elem in filelist:
fp = open( elem, 'w')
fp.write("Selam")
fp.close()
#

in case of any interpreter file pointer destructor failure but it
didnt change anything.
Do you have any idea why my files cannot be deleted from my disk with
2nd part of my code ?
 
T

Taygun Kekec

Allright i figured it out . Very stupid but very forgottable fact. I
should close the file before deleting it with shell command...
 
M

Matt Nordhoff

Taygun said:
Code :
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os

if os.name == 'nt':
OS_Selection = 0
elif os.name == 'posix':
OS_Selection = 1
else :
OS_Selection = 1

del_cmd_os = ( "del","rm")
filelist = ("ddd.txt","eee.txt","fff.txt")

# Creating Files
for elem in filelist:
open( elem, 'w').write("Selam")

#
# Removing Files
for elem in filelist:
fp = open( elem, 'r')
os.system( "%s %s" % (del_cmd_os[OS_Selection], fp.name))
fp.close()
#

Run this code on Windows XP , it says "the file is being used by
another process" and fails to delete the file. I tried replacing :
#
for elem in filelist:
open( elem, 'w').write("Selam")
#
with :
#
for elem in filelist:
fp = open( elem, 'w')
fp.write("Selam")
fp.close()
#

in case of any interpreter file pointer destructor failure but it
didnt change anything.
Do you have any idea why my files cannot be deleted from my disk with
2nd part of my code ?

Why are you executing another program just to delete a file?
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top