简体   繁体   English

从文件路径中提取文件

[英]extraction of file from filepath

I need to extract file name without extension name. 我需要提取不带扩展名的文件名。

example. 例。

/home/si/text.txt /home/si/text.txt

/home/si/text.vx.txt /home/si/text.vx.txt

In the both case I should receive output text only. 在两种情况下,我都应该只接收输出文本。 I am not sure how many trailing extension file can have but I need to extract only file name. 我不确定尾随扩展文件可以有多少,但是我只需要提取文件名。 I have tried spliitext(filename)[0] but it gave me output text.vx rather than text 我已经尝试过spliitext(filename)[0],但是它给了我输出text.vx而不是text

This should work for your needs: 这应该可以满足您的需求:

from os.path import basename
print basename("/home/si/text.vx.txt").split('.')[0]
>>> text

I use split function after getting file name. 我在获取文件名后使用了分割功能。

filename.split('.')[0] filename.split('。')[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM