是否有更好的方法使用std::path::Path使祖先在文件夹结构层次结构中上升几级?
目前,如果想要获得 5 级以上的祖先,我会执行以下操作(见下文),但我觉得一定有更好的方法。
我知道方法祖先,但我不知道如何使用它来改进这个片段。
use std::path::PathBuf;
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let ancestor_dir = out_dir
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
Run Code Online (Sandbox Code Playgroud)
这个可以写成更易读的方式吗?
我有任意嵌套的 JSON 对象,类似于下面的内容。
{
"parent1" : "someval"`,
"parent2" : {
"a" : "someval",
"b" : "someval"
},
"parent3" : {
"child1" : {
"a" : "someval"
},
"child2" : {
"b" : "someval"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要递归地遍历它们并检查是否有任何父项有子键a或b或两者,并生成该父项的 JSON 路径,如下所示:
Output:
parent2
parent3.child1
parent3.child2
Run Code Online (Sandbox Code Playgroud)
我尝试过使用
jq -r 'path(..) | map (. | tostring) | join (".")
Run Code Online (Sandbox Code Playgroud)
这可以帮助我生成所有路径,但我还没有找到一种方法来成功地组合诸如 之类的has("a")条件path。我怎样才能实现这一目标?
我正在使用 C# Path.Combine 方法来组合路径,如下所示
string str1 = "C:"; // This value can be read from configuration
string str2 = "\Dataaccess\bin"; // This value can be read from configuration
string combinedPath = Path.Combine(str1,str2);
Run Code Online (Sandbox Code Playgroud)
我的期望是组合路径应该返回“C:\ Dataaccess \ bin”但它返回\ Dataaccess \ bin
如果我将 str1 值更改为“C:”,如下所示,
string str1 = "C:\"; // This value can be read from configuration
string str2 = "\Dataaccess\bin"; // This value can be read from configuration
string combinedPath = Path.Combine(str1,str2);
Run Code Online (Sandbox Code Playgroud)
然后它也返回 \Dataaccess\bin
如果我将 str1 值更改为“C:”,然后将 str2 值更改为“Dataaccess\bin”,如下所示,
string str1 = …Run Code Online (Sandbox Code Playgroud) 我有一些javascript菜单代码可以从一个单独的目录中正常工作.但是,当我尝试从同一目录中调用相同的.js文件时,它将看不到文件.
以下工作来自另一个目录:
script type ="text/javascript"> var vbImgPath ="../ 00-Menu-Files /"
但是,如果我在同一个文件夹中执行此操作,我该怎么办?
解决方案(经过多次实验后编辑):
我试验了很多!最终只有一种解决方案可行:
"../00-Menu-Files/"
来自其他目录的相同的东西!很奇怪,没有其他方法可以在自己的目录中调用它.但我找不到其他可行的替代方案.
我构建了一个小脚本,用于解析给定文件类型的文件的目录,并将该位置(包括文件名)存储在数组中.看起来像这样:
def getFiles(directory)
arr = Dir[directory + '/**/*.plt']
arr.each do |k|
puts "#{k}"
end
end
Run Code Online (Sandbox Code Playgroud)
输出是路径和文件.但我只想要这条道路.
而不是/foo/bar.txt我只想要/ foo /
我的第一个想法是regexp,但我不知道该怎么做.
假设我有一个我正在部署的项目
www.foo.com/path1/default.aspx
和
www.foo.com/path2/default.aspx
什么是最可靠的方式来了解我是否在文件夹"path1"或"path2"?我可以直接获取它,或者我是否需要以某种方式在Request.Url.AbsolutePath上拆分()或者?
我只想根据用户所在的文件夹更改颜色等.
谢谢你的帮助!
问题1:我的Vim使用扩展名〜我的root进行备份
我的.vimrc中有以下行
set backup backupdir=~/tmp/vim//,~/tmp//,.//,/var/tmp//,/tmp//$
Run Code Online (Sandbox Code Playgroud)
但是,我在行中看不到根目录.
为什么我的Vim使用扩展名〜我的root来备份我的shell脚本?
问题2:我的Zsh在我的PATH登录时运行我的shell脚本.例如,我的"replaceUp"shell脚本在登录时从我的root开始.我默认将它保存在〜/ bin/shells/apps中.
为什么Zsh在登录时运行我的PATH中的shell脚本?
以下是在webb应用程序中调用图像的两种方法.
<img src="/myapp/img/world.gif" />
Run Code Online (Sandbox Code Playgroud)
要么
<img src="http://www.example.com/myapp/img/world.gif" />
Run Code Online (Sandbox Code Playgroud)
哪个最好使用或两者具有相同的含义.如果两者的含义不同,为什么呢?如果我在我的应用程序中使用第二种方法来调用所有文件(图像,swf,flv等),是否有任何性能限制
我正在努力创建一个脚本,为我创建图像库.
当我运行我所拥有的它告诉我
No such file or directory at photographycreate line 16.
------------
(program exited with code: 2)
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所获得的代码.
#!/etc/perl -w
#CHANGE THIS
$filecategory = "cooking";
$filenumber = 0;
#$filename = "photography";
$imagedirectory = "\"/media/New Volume/Programming/kai product/media/photography/".$filecategory."/images/\"";
$galleryfile = "\"/media/New Volume/Programming/kai product/pages/".$filenumber."_".$filecategory."_gallery.html\"";
@imagelocation = <$imagedirectory/*>; #*/
$filecount = @imagelocation;
while($filenumber < 3) {
open GALLERY, "+>", $galleryfile or die $!;
print GALLERY ($filecount."\n");
print GALLERY ($imagedirectory."\n");
print GALLERY ($galleryfile."\n");
close GALLERY;
++$filenumber;
}
Run Code Online (Sandbox Code Playgroud)
我想要它做的是创建文件,打开它,写东西,然后关闭/保存它.我怎么能用我所拥有的呢?
这是修复:
#!/etc/perl -w
use Fcntl; #The Module …Run Code Online (Sandbox Code Playgroud)