Python Forum

Full Version: Matching Duplicate file names with different extentions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I'm relatively new to python and need some help with creating a script that is able to pair duplicate file names with different extensions.

For example, say I have a image named 'test1.jpg' and a text file with description of that image named 'test1.txt' and multiple files just like that, with different numbers.

How can i group the two and make a new folder each time there is a match? So with a match for 'test1' there would be a folder named test1, which would include the image and text file. I have done a little research but have been unable to find anything I can use. Let me know what this would look like or direct me to a link that explains how to do this please Smile

Thanks in advance!
I would look at the os module and the glob module. They should have the tools you need to do this.
glob.glob(pattern) will return a list of file names matching that pattern. You can use it to check for each of this file but with .txt extension. If it exists - os.path.exists('test99.txt') for example.
If that happens use os.mkdir() to create a directory with a proper name and move the files there - os.rename().