请问您指定目录下的文件是关于什么主题的?
- 内容介绍
- 文章标签
- 相关推荐
本文共计204个文字,预计阅读时间需要1分钟。
pythondef list_files($dir='', $rec=True, $extensions=None): result=[] if not $rec: for file in os.listdir($dir): if $extensions and not any(file.endswith(ext) for ext in $extensions.split(',')): continue result.append(file) else: for file in os.listdir($dir): if os.path.isdir(os.path.join($dir, file)): result.extend(list_files(os.path.join($dir, file), True, $extensions)) elif $extensions and not any(file.endswith(ext) for ext in $extensions.split(',')): continue result.append(file) return result
返回指定目录下的文件/* * 返回指定目录下的文件 * @param string $dir 目录名 * @param boolean $rec 是否递归 * @param string 筛选扩展名 多个扩展名使用,号分隔 * @return array */ function treeFile($dir='', $rec=FALSE, $ext='') { $list = ARRAY(); $dir = ($dir == '') ? dirname(__FILE__) : $dir; if (!is_dir($dir)) return ARRAY(); $link = opendir($dir); while (FALSE !== ($file = readdir($link))) { if ($file != '.' && $file != '..') { $path = $dir.DIRECTORY_SEPARATOR.$file; if ($rec && is_dir($path)) { $list = array_merge($list, treeFile($path, $rec, $ext)); } else { if (!empty($ext)) { $extArr = explode(',', $ext); if (in_array(strrchr($file, '.'), $extArr)) $list[] = $path; } else { $list[] = $path; } } } } closedir($link); return $list; }
本文共计204个文字,预计阅读时间需要1分钟。
pythondef list_files($dir='', $rec=True, $extensions=None): result=[] if not $rec: for file in os.listdir($dir): if $extensions and not any(file.endswith(ext) for ext in $extensions.split(',')): continue result.append(file) else: for file in os.listdir($dir): if os.path.isdir(os.path.join($dir, file)): result.extend(list_files(os.path.join($dir, file), True, $extensions)) elif $extensions and not any(file.endswith(ext) for ext in $extensions.split(',')): continue result.append(file) return result
返回指定目录下的文件/* * 返回指定目录下的文件 * @param string $dir 目录名 * @param boolean $rec 是否递归 * @param string 筛选扩展名 多个扩展名使用,号分隔 * @return array */ function treeFile($dir='', $rec=FALSE, $ext='') { $list = ARRAY(); $dir = ($dir == '') ? dirname(__FILE__) : $dir; if (!is_dir($dir)) return ARRAY(); $link = opendir($dir); while (FALSE !== ($file = readdir($link))) { if ($file != '.' && $file != '..') { $path = $dir.DIRECTORY_SEPARATOR.$file; if ($rec && is_dir($path)) { $list = array_merge($list, treeFile($path, $rec, $ext)); } else { if (!empty($ext)) { $extArr = explode(',', $ext); if (in_array(strrchr($file, '.'), $extArr)) $list[] = $path; } else { $list[] = $path; } } } } closedir($link); return $list; }

