切割琴弦Pyhton
我有一个使用PYQT5创建的简单UI,
它加载了一个文件,让您选择一个输出文件夹并创建一个带有附加信息信息的新TXT文件。
加载文件的字符串写入
self.inputs.filename.text()
它的字符串看起来像“ c:/user/folder/file.txt”,
在应用程序后面我在特定位置中写入新文件中。
new_txt = open(self.inputs.foldername.text() + "/optimized.txt", "w")
我想将“ optimized.txt”字符串添加到原始文件名中。但是,如果我使用self.inputs.filename.tex(),它会给整个路径返回并产生错误。我使用.removesuffix()尝试了一下,但是由于路径总是可变的,因此我找不到一个解决方案,可以在最后一个“/”之后保持角色。
请不要私下我,我是Python的新手。
i have a simple ui created with pyqt5
it loads a file, let you choose a outputfolder and creats a new txt file with additonals information.
the string of the loaded file is written to
self.inputs.filename.text()
it looks like "C:/User/Folder/File.txt"
later in the application i write into a new file in a specific location.
new_txt = open(self.inputs.foldername.text() + "/optimized.txt", "w")
I want to add the "optimized.txt" string to the orginal Filename. But if I use self.inputs.filename.tex() it gives back the whole path and creates an error. I tried it with .removesuffix() but since the Path is always variable I cant find a solution to just keep the character after the last "/".
Please dont lynch me I'm quite new to python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用字符串的拆分函数在最后一个'/'之后获取元素。这样:
现在您可以使用应包含“ file.txt”的最后一个元素。您可以再次将其分配给“”。并仅获取没有.txt扩展名的文件的名称。
希望我回答你的问题。
You can use the split function of a string to get the element after the last '/'. Like this :
Now you can use the last element that should contains 'File.txt'. you can split it again respect to "." and get only the name of the file without the .txt extension.
Hope I answered you question.