Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 输入两个名称相同但扩展名不同的文件_Python_File - Fatal编程技术网

Python 输入两个名称相同但扩展名不同的文件

Python 输入两个名称相同但扩展名不同的文件,python,file,Python,File,我想通过只输入一个文件来输入两个名称相同但扩展名不同的文件: 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 = askope

我想通过只输入一个文件来输入两个名称相同但扩展名不同的文件:

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)的文件对象

之后,我调用一个函数,该函数使用两个文件对象


文件具有不同的扩展名,因为它们包含不同的信息,两个文件位于同一文件夹中。

将文件名转换为字符串,并使用
str.join()
添加文件结尾


除非文件结尾因情况不同而不同,否则这应该有效。

将文件名转换为字符串,并使用
str.join()
添加文件结尾


除非每个案例的文件结尾不同,否则这应该有效。

假设您已经知道一个文件名,例如:

file = 'test.rwh'
然后,您只需使用另一个文件扩展名
.row
即可:

file.replace('.rwh','.row')
这将给出
test.row
。如果有多个
.rwh
值,您可以使用Peter Wood的注释或使用正则表达式:

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

返回
test.rwh.row

假设您已经知道一个文件名,例如:

file = 'test.rwh'
然后,您只需使用另一个文件扩展名
.row
即可:

file.replace('.rwh','.row')
这将给出
test.row
。如果有多个
.rwh
值,您可以使用Peter Wood的注释或使用正则表达式:

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

允许您获取根名称:

>>> import os

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

>> root + '.row'
'/my/filename.row'
允许您获取根目录名称:

>>> import os

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

>> root + '.row'
'/my/filename.row'

文件名可能在结尾以外的其他位置有
.rwh
。请特别参阅。文件名可能在结尾以外的其他位置有
.rwh
。请特别参阅。