0
enter code here
import os
os.chdir('I:\\Movies')
files = os.popen('dir').readlines()
disk = raw_input("Enter the disk: ")
while disk != "done":
    os.chdir(disk + ':\\' + 'Movies')
    files_in_disk = os.popen('dir').readlines()
    for each_file in files_in_disk:
        for item in files:
            if ' '.join(each_file.split()[3:]) in item:
                each_file = ' '.join(each_file.split()[3:])
                os.system('rmdir /q /s ' + '"' + each_file + '"')
                break


    disk = raw_input("Enter the disk: ") 

I had two copies of the same movies on two different drives, I wrote this script to
delete one of the copy. But on E drive it erased nearly all of my files, why did this happen can someone please point out my mistake.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2189524
  • 21
  • 1
  • 4

3 Answers3

1

I think something here is not doing what you expect:

if ' '.join(each_file.split()[3:]) in item:

If a any file is has less than 4 space-delimited parts, the first bit of the if will be the empty string, and this will return true.

The problem is your loop. For each file in the E:\Movies, it checks if any file in I:\Movies matches (well, everything past the third word). If one of the files in I:\Movies happens to have less than 4 words (not entirely implausible), then the if will be true on every run.

I'm not sure what the intent is here, but this is my best guess as to what may be causing the problem.

beatgammit
  • 19,817
  • 19
  • 86
  • 129
  • hello, thank you for the answer, but I had already used a print statement to check whether it is doing as intended actually this statement extracts only the name of the folder from the whole string and checks whether it is a sub-string of each file in I drive.. – user2189524 Mar 20 '13 at 06:27
  • 1
    Have you considered using the features of Python? [os.listdir()](http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python), [shutil.rmtree()](http://stackoverflow.com/questions/13118029/deleting-the-folders-in-python-recursively), [os.path.dirname()](http://docs.python.org/2/library/os.path.html) and others. You don't have to shell out every time. – beatgammit Mar 20 '13 at 06:30
  • Despite your declaration otherwise, I think @tjameson is correct about the issue. Also, using `os.listdir` will give you a list of file names (not paths) as strings. Basically, you will be able to do file name comparison in a much simpler way. The only concern would be sub-directories. – Tim Bender Mar 20 '13 at 06:44
0

Your mistake was not initially running this program with a print each_file statement rather than immediately jumping to the rmdir command.

Though this may read as a snarky answer, it is truly meant to be helpful. Whenever making irreversible changes (like deleting items from a file system or DB), one should always take some step to verify that the appropriate instructions are being generated/executed.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
0

According to Microsft's TechNet article about rmdir:

/s : Removes the specified directory and all subdirectories including any files. Use /s to remove a tree.

So, if according to the other answers it is possible to supply non-matching file paths to rmdir it is not very difficult to delete whole subtrees on the disk. Especially if the list of files also contains subdirectories that point to parent subdirectories (for instance i:\movies..), you could be in a world of hurt in cases like that.

But I don't have access to a Windows machine with Python installed to prove it.

ramdyne
  • 177
  • 1
  • 9