Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

Is there a way to delete all duplicate files in the downloads folder using Python?

I am trying to traverse through every file in my downloads folder, deleting any duplicate files. But every time I run the script, I get the error: "FileNotFoundError: [WinError 2] The system cannot find the file specified: ... " Here is my code:


    import os
    
    path = 'C:/Users/walte/Downloads'
    
    for root,dirNames,fileNames in os.walk( path ):
        for file in fileNames:
            name, ext = os.path.splitext( file )
            if  name.endswith( ")" ):
                os.remove( file )

If I use print(file) instead of using os.remove, all duplicate files are listed. Here is the terminal output:

CS 499 Self-evaluation and reflection (1)
CS 499 Self-evaluation and reflection (2)
Detection (1)
Detection (2)
Detection (3)
JavaSetup8u241 (1)

That is causing confusing on why this isn't working. Thanks for the help!

question from:https://stackoverflow.com/questions/65931301/is-there-a-way-to-delete-all-duplicate-files-in-the-downloads-folder-using-pytho

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...