我想对目录中的一组文件夹执行RecursiveDirectoryIterator ./temp,然后根据文件夹的名称列出每个文件夹中的文件.
例如,我有文件夹A和B.
在A,我有文件说的列表,1.txt,2.php,2.pdf,3.doc,3.pdf.
在B,我有1.pdf,1.jpg,2.png.
我希望我的结果是这样的:
A => List of files in A
B => List of files in B
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?
<?php
$scan_it = new RecursiveDirectoryIterator("./temp");
foreach(new RecursiveIteratorIterator($scan_it) as $file =>$key) {
$filetypes = array("pdf");
$filetype = pathinfo($file, PATHINFO_EXTENSION);
if (in_array(strtolower($filetype), $filetypes)) {
$dlist=basename($file); //sort
?>
<ul>
<li>
<?php echo substr(dirname($file),11);?>
</li>
<li>
<a href="<?php echo $file;?>"><?php echo basename($file);?></a> …Run Code Online (Sandbox Code Playgroud) 我在C中使用scandir()函数,在一个文件夹,我需要获取文件名正好="exe"的文件.
如何过滤scandir返回的条目?
scandir的第三个参数是filter:
int scandir(const char *dirp, struct dirent ***namelist,
int (*filter)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
Run Code Online (Sandbox Code Playgroud)
它对我的目的有用吗?
在UNIX®系统线程参考中,在"线程安全"标题下的是"不保证在所有UNIX系统上都是线程安全的"功能列表.此列表中不存在函数scandir(),而列表中显示readdir().
然而,scandir()的glibc 源清楚地显示为调用readdir(),而不是线程安全的readdir_r().因为某些其他原因,scandir()在列表中被省略了,或者由于某种原因我是否缺少线程安全?
我有一个包含子文件夹的目录。我需要将所有这些以及它们的文件夹名称显示在一页上,因此如下所示:
echo Subfolder name
echo image, image, imageecho Subfolder2 name
echo image2, image2 , image2我试过使用
$images = glob($directory . "*.jpg");
Run Code Online (Sandbox Code Playgroud)
但是问题是我必须在中精确定义子文件夹名称$directory,例如“ path / folder / subfolder /”;
是否有类似“通配符”的选项可以检查所有子文件夹并回显foreach子文件夹名称及其内容?
另外,由于我无法控制的服务器限制opendir,scandir因此无法在此处应用。
我尝试在 Windows 上使用 os.scandir() 而不是 os.listdir() 来优化用 Python 编写的文件浏览功能。但是,时间保持不变,大约2分半钟,我不知道为什么。以下是原始和修改的功能:
os.listdir() 版本:
def browse(self, path, tree):
# for each entry in the path
for entry in os.listdir(path):
entity_path = os.path.join(path, entry)
# check if support by git or not
if self.git_ignore(entity_path) is False:
# if is a dir create a new level in the tree
if os.path.isdir( entity_path ):
tree[entry] = Folder(entry)
self.browse(entity_path, tree[entry])
# if is a file add it to the tree
if os.path.isfile(entity_path):
tree[entry] = File(entity_path)
Run Code Online (Sandbox Code Playgroud)
os.scandir() 版本: …
我在 pycharm 中有以下 python 代码:
# search for files or folders with two or more adjacent spaces
def twoplusspaces(path):
srchr = os.scandir(path) # get our iterator
# iterate through folders and files looking for adjacent blank spaces
for entry in srchr:
if " " in entry.name:
print(entry.name) # print name of file/folder with 2+ spaces
if entry.is_dir():
twoplusspaces(path + "\\" + entry.name) # recursive call when folder encountered
srchr.close() # done using iterator
Run Code Online (Sandbox Code Playgroud)
这按预期工作正常,但 pycharm 警告我entry.name和entry.is_dir()的“未解析的属性引用”。我觉得这很奇怪,因为Python文档说这些属性存在于由scandir()返回的DirEntry对象中:https ://docs.python.org/3/library/os.html#os.DirEntry
看起来我的 pycharm …
在Linux CentOS 5.5上使用PHP 5.3.3(稳定版).
这是我的文件夹结构:
www/myFolder/
www/myFolder/testFolder/
www/myFolder/testFile.txt
Run Code Online (Sandbox Code Playgroud)
使用scandir()对"myFolder"文件夹我得到以下结果:
.
..
testFolder
testFile.txt
Run Code Online (Sandbox Code Playgroud)
我正在尝试从结果中过滤掉文件夹,只返回文件:
$scan = scandir('myFolder');
foreach($scan as $file)
{
if (!is_dir($file))
{
echo $file.'\n';
}
}
Run Code Online (Sandbox Code Playgroud)
预期结果是:
testFile.txt
Run Code Online (Sandbox Code Playgroud)
但是我实际上看到了:
testFile.txt
testFolder
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这里出了什么问题?
TIA。凭借我的 PHP 补救技能,我无法弄清楚为什么 scandir 不能按字母顺序自动排序。(将文件夹组合在一起并按字母顺序排序以及将文件组合在一起并按字母顺序排序也很好,但这并不重要。)我错过了什么?
<?php
$dir = './customers/' . $customer . "/";
$exclude = array(".","..",".htaccess");
if (is_dir($dir)) {
$files = scandir($dir);
foreach($files as $key=>$dir){
if(!in_array($dir, $exclude)){
echo ("<a href=\"./customers/$customer/".$dir."\">".$dir."</a><br>");
}
}
}
?>
Run Code Online (Sandbox Code Playgroud) 你好,
我在名为 content 的目录中有以下文件:仅 index.php、page1.php、page2.php 和 page3.php。
然后我有这个代码:
$column = scandir("content");
foreach ($column as $value) {
$stvalue = str_replace(".php", "", $value);
echo "<div>$stvalue</div>";
}
Run Code Online (Sandbox Code Playgroud)
尽管如此,这就是我得到的:
<div>.</div>
<div>..</div>
<div>index</div>
<div>page1</div>
<div>page2</div>
<div>page3</div>
Run Code Online (Sandbox Code Playgroud)
前两个元素是什么?我没有这样命名的文件,所以我不明白。
谢谢你。
os.DirEntry除了列出包含目录之外,有没有人可以创建一个对象?
我想将其用于两个目的:
scandir ×10
php ×4
c ×2
python ×2
echo ×1
filesystems ×1
foreach ×1
io ×1
listdir ×1
pycharm ×1
python-3.x ×1
readdir ×1
sorting ×1
subdirectory ×1
windows ×1