如何得到文件所在的路径??

sea_zs 2009-04-14 07:46:28
现在知道文件的全路径,请问如何得到文件所在的目录?
FilePath=/home/zhangsan/***********.bin

如何得到***********.bin的所在路经,即/home/zhangsan/

大家都有什么方法,最好能用awk的match,请各位高手帮忙了!!
...全文
126 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
F1316633 2009-06-07
  • 打赏
  • 举报
回复
>>> import os
>>> filepath = "/abc/lksfjkfsd/ksdfjsdlk/xyz.bin"
>>> (root, filename) = os.path.split(filepath)
>>> print root + "/"
/abc/lksfjkfsd/ksdfjsdlk/
>>> print filename
xyz.bin
Aylazhang 2009-06-05
  • 打赏
  • 举报
回复
use Cwd;
print getcwd;
gift_lbs 2009-06-05
  • 打赏
  • 举报
回复
os.path.dirname(FilePath)
hjjdebug 2009-06-04
  • 打赏
  • 举报
回复
采用sed 脚本,如下面代码
[code=BatchFile]
$ cat 1.txt
FilePath=/home/zhangsan/***********.bin

$ cat 1.txt | sed 's/\(^.*\/\).*/\1/'
FilePath=/home/zhangsan/
[/code]
buzuibuxiu 2009-05-28
  • 打赏
  • 举报
回复
GetWindowRectory()
Getsystemdirectory()
Getfullpathname()
这些api是整合在Windows里的,大部分语言都可以调用!
bgrobert 2009-05-28
  • 打赏
  • 举报
回复
初学perl,当做练习,有点粗糙,临时写的。文件、目录还是用Perl的模块比较好。
#!/usr/bin/perl
$_=$ARGV[0];
s/^\s//;
s/\s$//;
print "$1" if (/\w+=(.*)\/.*\.\w+/);

测试结果:
F:\code\perl>perl FilePath.pl FilePath=/home/zhangsan/*******.bin
/home/zhangsan
boys2002 2009-05-21
  • 打赏
  • 举报
回复
顶3楼
lixq2000 2009-04-18
  • 打赏
  • 举报
回复
获取文件的路径os.path.dirname(path)
如果想要获取绝对路径os.path.abspath()
kingteng 2009-04-14
  • 打赏
  • 举报
回复
python的os.path模块有这些API函数,比如os.path.basename(path)
和os.path.dirname(path)

iambic 2009-04-14
  • 打赏
  • 举报
回复
用dirname命令:
[code=BatchFile]dirname $FilePath[/code]

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧