Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting subdirectories

Status
Not open for further replies.

beveronic

Technical User
Dec 17, 2004
10
DE
Hi,
I have the following problem: I want to delete a subdirectory but after deleting and rescaning the files the subdirectory is still there.When I look in my directory it is gone, so why does it find it again?
def rescan():
scan_Files=[]
def walker(arg, dir, path):
scan_files.append(dir)
for val in path:
pathname = dir + os.sep + val
if os.path.isfile(pathname) == 1:
scan_files.append(dir + os.sep + val)

os.path.walk(pathDir, walker, None)
return scan_files
rescan()
def recursive_delete(dirname):
files = os.listdir(dirname)
for file in files:
path = os.path.join (dirname, file)
if os.path.isdir(path):
recursive_delete(path)
else:
retval = os.unlink(path)
os.rmdir(dirname)
recursive_delete("/home/tools/beveronic")
rescan()
 
I had a similar problem with parsing a file. I parsed it looking for one set of values, then when I went back and parsed it again for a second set, the array wasn't getting reset and the new values were appended to the end.

My problem was with class variables, but I think yours might be similar because of scoping issues I don't fully understand.

My recommendation would be to simplify your code. First, get rid of the nested function definitions, those can cause scoping problem, especially since you're not passing the array into the nested function but relying on it on it being accessible when the os.path.walk calls your function.

Second, print out your scan_files array before and after you set it to "[]" and also on entry and exit of the walker function, my bet is that is isn't getting reset.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top