Python Forum
Matching Duplicate file names with different extentions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matching Duplicate file names with different extentions
#1
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!
Reply
#2
I would look at the os module and the glob module. They should have the tools you need to do this.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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().
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matching string from a file tester_V 5 457 Mar-05-2024, 05:46 AM
Last Post: Danishhafeez
  rename same file names in different directories elnk 0 719 Nov-04-2022, 05:23 PM
Last Post: elnk
  Saving Excel workbook file with dataframe names Biplab1985 0 2,048 Jun-07-2020, 12:25 PM
Last Post: Biplab1985
  Finding Duplicate in CSV file bond009 3 2,388 May-14-2020, 05:37 AM
Last Post: bond009
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 5,733 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  splitstring file names a by hyphen steve22020 3 3,302 Oct-30-2019, 05:39 PM
Last Post: steve22020
  Is there a more effecient way to do this ? File Names sumncguy 2 2,098 Jul-11-2019, 12:47 PM
Last Post: DeaD_EyE
  How to combine file names into a list from multiple directories? python_newbie09 3 5,223 Jul-09-2019, 07:38 PM
Last Post: python_newbie09
  Duplicate output when calling a custom function from the same file? road2knowledge 2 2,398 May-10-2019, 07:58 AM
Last Post: road2knowledge
  write each line of a text file into separate text files and save with different names Shameendra 3 2,804 Feb-20-2019, 09:07 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020