python正则表达式从路径中取文件名出来不加后缀(txt)

 1 正则表达式[^\\/:*?"<>|\r\n]+$    ---->取文件名包括后缀
 2 e.g.  >>>D:\PyCharm 2018.2.4\pythonWork\venv\Scripts\python.txt
 3         ->python.txt
 4 
5 正则表达式(.+?)\.txt ---->取上次结果去掉后缀 6 e.g. >>>python.txt 7 ->python

8 部分代码应用截取>>> 9 def send(): 10 sendfile = tk.Tk() 11 sendfile.withdraw() 12 13 file_path = filedialog.askopenfilename() 14 print(file_path) 15 16 newName = re.findall(r'[^\\/:*?"<>|\r\n]+$',file_path) 17 newName = re.findall(r'(.+?)\.txt',newName[0]) 18 19 os.system('python markup.py < %s > d:/%s.html '%(file_path,newName[0]))

 

取路径中最后一个‘/’前的所有内容包括最后一个‘/’

1 newNamepath = re.findall(r'(.+/).+?\.',file_path)
2 
3     print(newNamepath[0]) # 打印文件的路径
4 
5 
6 e.g.>>>D:/PyCharm 2018.2.4/pythonWork/item1/test_input.txt
7       
8        -->D:/PyCharm 2018.2.4/pythonWork/item1/

 

posted @ 2019-03-17 15:06  张玉娘  阅读(1619)  评论(0编辑  收藏  举报