1

I want to input two files with same name but different extension by doing the input of just one:

tkMessageBox.showinfo(title="Info",message="Please input both the .rwh file")

# the filetype mask (default is all files)
mask = \
[("files","*.rwh"),
 ("All files","*.*")]

title = 'Open'                
files = askopenfilenames(initialdir=self.initial_dir, filetypes=mask,title=title)

Then the part that I do not how to do. It has to create a file object by reading the file input and then by reading the name create another one with same name but different extension (.row).

Afterwards I call a function which uses both files object.

The files have different extension because they contain different information, both files are in the same folder.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
gis20
  • 1,024
  • 2
  • 15
  • 33

3 Answers3

3

os.path.splitext allows you to get the root name:

>>> import os

>>> filename = '/my/filename.rwh'
>>> root, ext = os.path.splitext(filename)
>>> root
'/my/filename'

>> root + '.row'
'/my/filename.row'
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
2

Assuming you already know one file name, for example:

file = 'test.rwh'

then you simply could replace it with .row, the other file extension:

file.replace('.rwh','.row')

this gives test.row. In the case of having multiple .rwh values you can use Peter Wood's comments or use for example regular expressions:

import re
file = 'test.rwh.rwh'
re.sub('.rwh$','.row',file)

returns test.rwh.row.

agold
  • 6,140
  • 9
  • 38
  • 54
  • 2
    It's possible that the filename could have `.rwh` in other positions than at the end. – Peter Wood Nov 02 '15 at 11:38
  • 1
    See [Right-to-left string replace](http://stackoverflow.com/questions/9943504/right-to-left-string-replace-in-python), particularly [this answer](http://stackoverflow.com/a/9943875/1084416). – Peter Wood Nov 02 '15 at 11:41
0

Convert the file name to a string and use str.join() to add the file endings.

This should work unless the file endings differ from case to case.

poppyseeds
  • 421
  • 1
  • 7
  • 14