C产品在市场上有哪些独特优势?
- 内容介绍
- 文章标签
- 相关推荐
本文共计491个文字,预计阅读时间需要2分钟。
python获取文件名(包括全路径)和后缀格式,遍历子文件夹def get_files(path, suffix): # 文件夹路径 folder_path=path # 后缀格式 file_suffix=suffix # 文件列表 file_list=[]
# 遍历文件夹 for root, dirs, files in os.walk(folder_path): for file in files: # 检查文件后缀 if file.endswith(file_suffix): # 获取文件完整路径 full_path=os.path.join(root, file) # 添加到文件列表 file_list.append(full_path)
# 返回文件列表 return file_list
1、编写获取文件名(全路径)子函数
/////param ///path:文件夹路径 ///suffix:后缀格式, 如bmp,txt ///fileList:文件名存放 ///isSubcatalog:true遍历子文件夹,否则不遍历 void getFiles(string path, string suffix, ref List<string> fileList, bool isSubcatalog) { string filename; DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] file = dir.GetFiles(); //DirectoryInfo[] dii = dir.GetDirectories();//如需遍历子文件夹时需要使用 foreach (FileInfo f in file) { filename = f.FullName; if (filename.EndsWith(suffix))//判断文件后缀,并获取指定格式的文件全路径增添至fileList { fileList.Add(filename); } } 获取子文件夹内的文件列表,递归遍历 if(isSubcatalog) { foreach (DirectoryInfo d in dii) { getFiles(d.FullName, fileList); } } return; }
2、在界面中放置一个button控件,单击按钮时弹出文件夹路径选择窗口,并调用getFiles子函数:
List<string> imageFiles = new List<string>(); private void btnSelectPath_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "Please choose image path."; DialogResult result = dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { return; } string folderPath = dialog.SelectedPath.Trim(); DirectoryInfo theFolder = new DirectoryInfo(folderPath); if (theFolder.Exists) { getFiles(folderPath,"bmp", ref imageFiles, false); return; } }
以上实例代码大家可以本机测试下,感谢大家的学习和对易盾网络的支持。
本文共计491个文字,预计阅读时间需要2分钟。
python获取文件名(包括全路径)和后缀格式,遍历子文件夹def get_files(path, suffix): # 文件夹路径 folder_path=path # 后缀格式 file_suffix=suffix # 文件列表 file_list=[]
# 遍历文件夹 for root, dirs, files in os.walk(folder_path): for file in files: # 检查文件后缀 if file.endswith(file_suffix): # 获取文件完整路径 full_path=os.path.join(root, file) # 添加到文件列表 file_list.append(full_path)
# 返回文件列表 return file_list
1、编写获取文件名(全路径)子函数
/////param ///path:文件夹路径 ///suffix:后缀格式, 如bmp,txt ///fileList:文件名存放 ///isSubcatalog:true遍历子文件夹,否则不遍历 void getFiles(string path, string suffix, ref List<string> fileList, bool isSubcatalog) { string filename; DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] file = dir.GetFiles(); //DirectoryInfo[] dii = dir.GetDirectories();//如需遍历子文件夹时需要使用 foreach (FileInfo f in file) { filename = f.FullName; if (filename.EndsWith(suffix))//判断文件后缀,并获取指定格式的文件全路径增添至fileList { fileList.Add(filename); } } 获取子文件夹内的文件列表,递归遍历 if(isSubcatalog) { foreach (DirectoryInfo d in dii) { getFiles(d.FullName, fileList); } } return; }
2、在界面中放置一个button控件,单击按钮时弹出文件夹路径选择窗口,并调用getFiles子函数:
List<string> imageFiles = new List<string>(); private void btnSelectPath_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "Please choose image path."; DialogResult result = dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { return; } string folderPath = dialog.SelectedPath.Trim(); DirectoryInfo theFolder = new DirectoryInfo(folderPath); if (theFolder.Exists) { getFiles(folderPath,"bmp", ref imageFiles, false); return; } }
以上实例代码大家可以本机测试下,感谢大家的学习和对易盾网络的支持。

