我有以下变量:
input_file = 'gs://tinydancer/test_files/GoogleSheetsandPython_1min.flac'
我想使用input文件变量的GoogleSheetsandPython_1min
部分来命名.txt文件。
稍后将在脚本中创建。你知道吗
我还想在文件名后面加上.txt。你知道吗
以下是我目前是如何做到这一点的:
text_file = open("GoogleSheetsandPython_1min.txt", "a")
通过简单的硬编码,我想让它自动化。因此,一旦设置了输入文件,就可以使用它相应地更改output.txt文件名。我对此做了一些研究,但到目前为止还没有找到好的方法。你知道吗
您可以使用
os.path.basename
您可以沿着
/
分割字符串并获取最后一项,然后像这样附加“.txt”:如果我误解了,您想用
.txt
替换.flac
,您可以对.
进行另一次拆分,然后附加.txt
。你知道吗正则表达式解决方案:
然后可以在
.
上拆分以除去文件扩展名。你知道吗使用
os.path
相关问题 更多 >
编程相关推荐