dirname

Python idiom to get same result as calling os.path.dirname multiple times?

ぃ、小莉子 提交于 2021-02-07 11:49:57
问题 I find myself needing to get a parent directory of a python file in a source tree that is multiple directories up with some regularity. Having to call dirname many times is clunky. I looked around and was surprised to not find posts on this. The general scenario is: import os.path as op third_degree_parent = op.dirname(op.dirname(op.dirname(op.realpath(__file__)))) Is there a more idiomatic way to do this that doesn't require nested dirname calls? 回答1: Normalize a relative path; os.pardir is

Python idiom to get same result as calling os.path.dirname multiple times?

大兔子大兔子 提交于 2021-02-07 11:49:24
问题 I find myself needing to get a parent directory of a python file in a source tree that is multiple directories up with some regularity. Having to call dirname many times is clunky. I looked around and was surprised to not find posts on this. The general scenario is: import os.path as op third_degree_parent = op.dirname(op.dirname(op.dirname(op.realpath(__file__)))) Is there a more idiomatic way to do this that doesn't require nested dirname calls? 回答1: Normalize a relative path; os.pardir is

linux basename dirname

混江龙づ霸主 提交于 2021-01-30 06:51:11
basename #只获取文件名 有两个参数 -s 和 -a, -s 要去掉的后缀 #表示指定要去掉的后缀 -a 表示多个名称 dirname #只获取文件的文件夹名 来源: oschina 链接: https://my.oschina.net/u/2308739/blog/545804

dirname ${BASH_SOURCE[0]}

自作多情 提交于 2020-03-27 15:25:59
3 月,跳不动了?>>> 例如在目录/root/aaa/bbb下,存在脚本文件test.sh, 脚本文件中存在这样一句话, BD=`dirname ${BASH_SOURCE[0]} ` ,意思就是说获取脚本文件test.sh所在目录的路径,但在不同目录下执行脚本test.sh,BD的值会有所不同。 1、在/root/aaa/bbb下,执行test.sh,BD的值为 . 2、在/root/aaa/下,执行test.sh,BD的值为./bbb 3、在/root下,执行test.sh,BD的值为./aaa/bbb 其中,BASH_SOURCE[0]代表脚本文件所在目录及文件名;dirname命令是获取文件所在目录。 如果脚本文件所在目录的绝对路径,可以结合pwd命令,具体如下所示: BD=`dirname ${BASH_SOURCE[0]}` cd $BD BASEDIR=`pwd` ,BASEDIR即为脚本文件test.sh的绝对目录,也即是/root/aaa/bbb。 来源: oschina 链接: https://my.oschina.net/u/1863491/blog/760484

How can I determine the current directory name in R?

邮差的信 提交于 2019-12-19 05:06:24
问题 The only solution I've encountered is to use regular expressions and recursively replace the first directory until you get a word with no slashes. gsub("/\\w*/","/",gsub("/\\w*/","/",getwd())) Is there anything slightly more elegant? (and more portable?) 回答1: Your example code doesn't work for me, but you're probably looking for either basename or dirname : > getwd() [1] "C:/cvswork/data" > basename(getwd()) [1] "data" > dirname(getwd()) [1] "C:/cvswork" 回答2: If you didn't know basename (and

How to get the cwd in a shell-dependend format?

半世苍凉 提交于 2019-12-13 07:43:11
问题 Since I'm using both Windows' cmd.exe and msysgit's bash , trying to access the Windows-path output by os.getcwd() is causing Python to attempt accessing a path starting with a drive letter and a colon, e.g. C:\ , which bash correctly determines an invalid unix-path, which instead should start with /c/ in this example. But how can I modify a Windows-path to become its msys-equivalent iff the script is running within bash ? 回答1: Ugly but should work unless you create an environment variable

Python os.path.dirname returns unexpected path when changing directory

送分小仙女□ 提交于 2019-12-11 12:13:45
问题 Currently I do not understand, why pythons os.path.dirname behave like it does. Let's assume I have the following script: # Not part of the script, just for the current sample __file__ = 'C:\\Python\\Test\\test.py' Then I try to get the absolute path to the following directory: C:\\Python\\doc\\py With this code: base_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)) + '\\..\\doc\\py\\') But why does the method os.path.dirname does not resolve the path, and print out ( print

OS X bash: dirname

北慕城南 提交于 2019-12-04 18:54:05
问题 I want to create a simple bash script to launch a Java program on OS X. The names of the file, the file path, and the immediate working folder all contain spaces. When I do this: #!/bin/sh cd `dirname $0` I get usage: dirname path I have also tried putting quotes in all kinds of different places. The most elaborate example being cd "`dirname \"$0\"`" Nothing has worked. I either get error messages or that cryptic "usage: dirname path" message. What are other methods that might work? Edit:

OS X bash: dirname

大憨熊 提交于 2019-12-03 12:25:52
I want to create a simple bash script to launch a Java program on OS X. The names of the file, the file path, and the immediate working folder all contain spaces. When I do this: #!/bin/sh cd `dirname $0` I get usage: dirname path I have also tried putting quotes in all kinds of different places. The most elaborate example being cd "`dirname \"$0\"`" Nothing has worked. I either get error messages or that cryptic "usage: dirname path" message. What are other methods that might work? Edit: this doesn't seem to be an issue for anyone but me so it must just be my box. I'm going to accept my own

Linux dirname 和basename命令

99封情书 提交于 2019-12-03 11:33:33
一、dirname指令 1、功能:从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的部分) 2、用法:dirname filename 例如下面几个例子 (1)# dirname /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts (2)# dirname /boot/grub/grub.conf /boot/grub (3)# dirname /etc/inittab /etc (4)# echo `pwd` && dirname modprobe.conf /etc . (5)#echo `pwd` && dirname sysconfig/network-scripts/ /etc sysconfig Linux系统管理 二、basename指令 1、功能:从给定的包含绝对路径的文件名中去除左边目录部分或者同时去除某个后缀的内容(目录的部分),然后返回剩下的部分(非目录的部分) 2、用法:basename filename [suffix] 例如下面几个例子 (1)#basename /etc/sysconfig/network-scripts/ifcfg-eth0 ifcfg-eth0 (2)#basename /boot/grub/grub