- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在为一个学校项目编写脚本:
search_folder = raw_input('Choose Directory')
for root, dirs, files in os.walk(search_folder):
for file in files:
pathname = os.path.join(root, file)
print pathname
print os.path.getsize(pathname)
如何对扩展上的输出进行排序?
最佳答案
使用sorted()
与 os.path.splitext()
lambda在 sort-by key
参数中。
import os
search_folder = '/Users/temp/'
results = []
for root, dirs, files in os.walk(search_folder):
for f in files:
results.append(os.path.join(root, f))
sorted_results = sorted(results, key=lambda x: os.path.splitext(x)[1])
for path in sorted_results:
print path
print os.path.getsize(path)
<小时/>
/Users/temp/.DS_Store
6148
/Users/temp/.localized
0
/Users/temp/profile.png
81168
/Users/temp/IMG_0115.xcf
113212
/Users/temp/profile.xcf
535202
您还可以使用list comprehension (为了速度和可读性)遍历文件:
results = [os.path.join(root, f)
for root, dirs, files in os.walk(search_folder)
for f in files]
排序而不存储suggestion来自@PM2Ring:
for root, dirs, files in os.walk(search_folder):
for f in sorted(files, key=lambda x: tuple(reversed(os.path.splitext(x)))):
pathname = os.path.join(root, f)
print pathname
print os.path.getsize(pathname)
关于python 对文件的扩展名/名称进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003624/
我有一个 .crt 文件。打开那个文件,我看到它以开头 -----BEGIN CERTIFICATE----- 如何从这个文件生成这 2 个文件?: 以-----BEGIN CERTIFICATE R
我有一个文件被加载到 fd.ChosenName 并想知道是否有一个快速的解决方案,例如与 stringbuilder 相反的截断文件名(扩展名)以用于显示目的。因此,如果 fd.ChosenName
我的文件名如下:name1.csv,我想提取该字符串的两个子字符串。一种将 name1 存储在一个变量中,另一种将扩展名 csv 存储在另一个变量中,但不带点。 我一直在寻找是否有像 Java 的 i
我正在尝试找出一种方法来确定我在 struts2 应用程序中上传的文件的文件类型。我的应用程序可以读取 CSV 和 XML 文件并根据上传的文件类型执行操作。我将该文件作为“File”类的实例。该文件
如何使用 NLP/信息提取 (IE) 方法检测/找出首字母缩略词的含义(扩展名)? 我们希望检测自由文本中是否使用了单词或其首字母缩略词,并将其映射到相同的实体/标记。 大多数在线论文都是关于医学缩略
我正在编写一个 shell 脚本,该脚本需要改变其行为,并根据特定 X11 扩展的存在或不存在为被调用程序提供不同的选项。我有一个可行的解决方案,但我希望有一个更干净的解决方案。我愿意考虑一个简单的
我要查找文件名的扩展名,扩展名只能是3个字符。 有点像, filename = str(input("Please enter filename: ")) 然后我想执行一个任务来查找扩展名,如果扩展名
我正在开发一种文档管理系统。最终用户是企业级用户。 我目前仅检查并允许上传符合以下条件之一的文件: "png|jpe?g|gif|xls|doc|docx|csv|ppt|txt|pdf|rtf" 我
尝试将文件列表添加到 Jlist,然后过滤 JList 中的文件以仅返回 .txt 文件和固定字符长度。还尝试删除返回的文件路径,并仅在文件 JList 中显示文件名+扩展名。 到目前为止,除了删除文
File.py file = fields.Binary('File') @api.multi def get_file(self): if self.file: xsl_fi
我尝试使用导航 Controller 使我的应用程序处于纵向模式我收到此错误 iam usring Xcode7 and Swift 2 And Target System IOS 9.3 声明仅在文
这个问题在这里已经有了答案: Test whether a glob has any matches in Bash (22 个回答) 关闭去年。 您将如何使用 bash 判断目录中是否存在特定扩展名
我正在开发转换器服务,我使用 FFMPEG 转换视频。 我想检查输入文件,哪种真的是视频。 我使用以下命令并获取文件类型。但是如果文件已经被操作过就无法理解了: curl -s -o /dev/nul
我需要一个规则来添加 .html 扩展名,只要有“不”结尾的斜杠。 一个新客户最近更改了电子商务脚本,新版本以不同的方式处理 SEO,并更改了他们所有 16,000 多个产品链接。这在站点被重新索引之
我正在尝试使用 Kickstarter 进行扩展以覆盖页面的正常呈现,并呈现 PDF 文件。为此我使用 FPDF。但我不确定该怎么做。我尝试这样做,但没有成功: AddPage();
在寻找巴恩斯利蕨分形的实现时,我遇到了一个具有 .pde 扩展名的实现。哪种编程语言使用此扩展? Implementation Page 最佳答案 此代码来自Processing.org,一个基于Ja
Visual Studio Code (1.22.2) 提供了一个名为 .dockerfile 的文件扩展名。在保存对话框中。带有此扩展名的文件是什么? Dockerfile 包含在所有文档和示例中,
我正在为32位计算机编译一个应用程序,因为该应用程序的创建者将其设置为64位。我确保所有库和DLL都是32位的。但是,我在编译时遇到问题。当我使用sh make.sh进行编译时,当它进入GCC构建部分
当我将文件保存在 img/upload 文件夹中时,文件会以正确的文件扩展名保存。 但是,当我尝试下载该文件时,会附加一个 .htm 文件扩展名。 我怎样才能避免这种情况?我在下面添加了我的代码; V
GHC 有几种有用的语言 extensions用于机械派生各种常见的 Haskell 类型类(-XDeriveFunctor、-XDeriveFoldable、-XDeriveTraversable)
我是一名优秀的程序员,十分优秀!