将文件名与路径分开 [英] Separate file name from path

查看:79
本文介绍了将文件名与路径分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其路径是这样的:D:\ Documents and Settings \ user \ Desktop \ Folder \ File1.txt.此处 File1.txt 是文件名.现在,我想将此文件名与文件路径分开.如果有,

I have a file whose path is like this : D:\Documents and Settings\user\Desktop\Folder\File1.txt. Here File1.txt is file name. Now I want to separate this file name from the file path. If I have,

路径= C:\ Documents and Settings \ user \ Desktop \ Folder \ File1.txt

然后我要将文件的这个名称存储在文件名中.因此输出将如下所示: filename = File1.txt 我对此一无所知.我是python的新手..有人可以在python中帮助我吗????

then I want to store this name of the file in to filename. so output will look like this : filename=File1.txt I am not getting any idea about this. I am new in python..can anybody help me please in python...???

推荐答案

使用 os 模块

import os 
fileName = os.path.basename(path)

这篇关于将文件名与路径分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆