[英]How to differentiate files with same name with different extension from two different folders? Python
[英]Input two files with same name but different extension
我只想输入一个,即可输入两个名称相同但扩展名不同的文件:
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)
那部分我不怎么做。 它必须通过读取文件输入然后通过读取名称来创建文件对象,以创建另一个具有相同名称但扩展名(.row)的对象。
之后,我调用一个使用两个文件对象的函数。
这些文件具有不同的扩展名,因为它们包含不同的信息,两个文件都位于同一文件夹中。
os.path.splitext
允许您获取根名称:
>>> import os
>>> filename = '/my/filename.rwh'
>>> root, ext = os.path.splitext(filename)
>>> root
'/my/filename'
>> root + '.row'
'/my/filename.row'
假设您已经知道一个文件名,例如:
file = 'test.rwh'
那么您只需将其replace
为另一个文件扩展名.row
:
file.replace('.rwh','.row')
这给出了test.row
。 在具有多个.rwh
值的情况下,您可以使用Peter Wood的注释或使用例如正则表达式:
import re
file = 'test.rwh.rwh'
re.sub('.rwh$','.row',file)
返回test.rwh.row
。
将文件名转换为字符串,然后使用str.join()
添加文件结尾。
除非文件结尾因大小写不同而不同,否则这应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.