我试图从一个名为../health/的文件中显示一个随机页面.在这个文件中有一个index.php文件和118个其他名为php文件的文件.我想从健康文件夹中随机显示一个文件,但我希望它排除index.php文件.
以下代码有时包含index.php文件.我也尝试改变$ exclude行来显示../health/index.php,但仍然没有运气.
<?php
$exclude = array("index.php"); // can add more here later
$answer = array_diff(glob("../health/*.php"),$exclude);
$whatanswer = $answer[mt_rand(0, count($answer) -1)];
include ($whatanswer);
?
Run Code Online (Sandbox Code Playgroud)
我尝试过的另一个代码如下
<?php
$exclude = array("../health/index.php"); // can add more here later
$health = glob("../health/*.php");
foreach ($health as $key => $filename) {
foreach ($exclude as $x) {
if (strstr($filename, $x)) {
unset($whathealth[$key]);
}
}
}
$whathealth = $health[mt_rand(0, count($health) -1)];
include ($whathealth);
?>
Run Code Online (Sandbox Code Playgroud)
此代码还包括index.php文件,而不是显示它将页面显示为错误的页面.
我正在编写一个自定义文件系统爬虫,它通过sys.stdin传递数百万个glob进行处理.我发现在运行脚本时,它的内存使用量随着时间的推移而大量增加,整个事情几乎停止了.我在下面写了一个小例子来说明问题所在.我做错了什么,或者我在Python/glob模块中发现了一个错误?(我使用的是python 2.5.2).
#!/usr/bin/env python
import glob
import sys
import gc
previous_num_objects = 0
for count, line in enumerate(sys.stdin):
glob_result = glob.glob(line.rstrip('\n'))
current_num_objects = len(gc.get_objects())
new_objects = current_num_objects - previous_num_objects
print "(%d) This: %d, New: %d, Garbage: %d, Collection Counts: %s"\
% (count, current_num_objects, new_objects, len(gc.garbage), gc.get_count())
previous_num_objects = current_num_objects
Run Code Online (Sandbox Code Playgroud)
输出如下:
(0) This: 4042, New: 4042, Python Garbage: 0, Python Collection Counts: (660, 5, 0) (1) This: 4061, New: 19, Python Garbage: 0, Python Collection Counts: (90, 6, 0) (2) This: …
我讨厌看到我的存储库中的每个目录列出每个文件两次,一次在前面有一个点,一次没有.我尝试添加.*到我的.hgignore文件,但它没有任何效果.这是错误的语法,更重要的是,首先尝试这个是一个坏主意吗?谢谢.
在bash我尝试glob从目录中的文件列表,以作为程序的输入.但是,我还想给这个程序提供文件名列表
files="/very/long/path/to/various/files/*.file"
Run Code Online (Sandbox Code Playgroud)
所以我可以这样使用它.
prompt> program -files $files -names $namelist
Run Code Online (Sandbox Code Playgroud)
如果glob给我:
/very/long/path/to/various/files/AA.file /very/long/path/to/various/files/BB.file /very/long/path/to/various/files/CC.file /very/long/path/to/various/files/DD.file /very/long/path/to/various/files/ZZ.file
Run Code Online (Sandbox Code Playgroud)
我想获得AA BB CC DD ZZ的列表,以便在没有长路径名和文件扩展名的情况下提供我的程序.但是我不清楚如何从那里开始!任何提示非常感谢!
我要循环匹配扩展名的文件jpg或txt.我用:
for file in myDir/*.{jpg,txt}
do
echo "$file"
done
Run Code Online (Sandbox Code Playgroud)
问题:如果目录根本不包含jpg文件,则循环将有一次带输出的迭代myDir/*.jpg.我认为*将被任意文件替换(如果不存在文件,则无法扩展).如何避免不必要的迭代?
假设您有这个目录树:
\n$ tree /tmp/test\n/tmp/test\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir_a\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir a\\012file with CR\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir a file with spaces\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 sub a directory\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 with a file in it\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir_b\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir b\\012file with CR\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dir b file with spaces\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir_c\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 \\012\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dir c\\012file with CR and *\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dir c file with space and *\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file_1\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file_2\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file_3\n\n4 directories, 11 files\nRun Code Online (Sandbox Code Playgroud)\n(这是一个生成该脚本的脚本。它\\012使\\n脚本编写更具挑战性。.hidden那里也有一个文件。)
Bash 5.1、zsh 5.8、Python pathlib 5.10、启用递归的 Python …
我想添加一个文件,该文件具有唯一的文件名,但前面的路径很长(例如a/b/c/d/filename.java).通常我会通过这样做将它添加到我的存储库中
git add *filename.java.
不过我之前也这样做过:
git add a/b/c/d/filename*
所以我试着将两者结合起来:
git add *filename*
但这确实很奇怪.它添加了每个未跟踪的文件.我可以看到失败的可能原因,但它们都应该出现在前两个命令之一中,所以我不知道为什么会发生这种情况.
我的问题不在于如何使用文件名将文件添加到git存储库(尽管这很有用).我的问题是我对*操作的误解是什么让我认为上述应该有效.
信息:
我正在使用Git Bash for Windows,它基于minGW.
我一直在使用zsh globbing来执行以下命令:
vim **/filename
vim *.html.erb
Run Code Online (Sandbox Code Playgroud)
等等,但当我输入类似的东西时:
find . -name *mobile*
Run Code Online (Sandbox Code Playgroud)
我收到了回复:
zsh: no matches found: *mobile*
Run Code Online (Sandbox Code Playgroud)
为什么?
我只是想给出一个带有通配符路径的文件名列表.
my $path = "/foo/bar/*/*.txt";
my @file_list = glob($path);
foreach $current_file (@file_list) {
print "\n- $current_file";
}
Run Code Online (Sandbox Code Playgroud)
大多数情况下这很好用,但是如果有一个大于2GB的文件,在某个/ foo/bar/*子路径中,glob会返回一个没有任何错误或警告的空数组.
如果我删除文件文件或添加如下字符/括号序列:
my $path = "/foo/bar/*[0-9]/*.txt";
Run Code Online (Sandbox Code Playgroud)
要么
my $path = "/foo/bar/*1/*.txt";
Run Code Online (Sandbox Code Playgroud)
然后glob再次运作.
更新:
这是一个例子(对于业务策略,我必须屏蔽路径名):
[root]/foo/bar # ls -lrt
drwxr-xr-x 2 root system 256 Oct 11 2006 lost+found
drwxr-xr-x 2 root system 256 Dec 27 2007 abc***
drwxr-xr-x 2 root system 256 Nov 12 15:32 cde***
-rw-r--r-- 1 root system 2734193149 Nov 15 05:07 archive1.tar.gz
-rw-r--r-- 1 root system 6913743 Nov 16 05:05 …Run Code Online (Sandbox Code Playgroud)