python - 在 os.walk() 期间找到绝对路径的更好方法?

标签 python python-2.7 file-io python-os

我正在练习 os 模块,更具体地说是 os.walk()。我想知道是否有一种更简单/更有效的方法来查找文件的实际路径,考虑到这会产生一条路径,表明当 os.walk() 首次运行时文件位于原始文件夹中:

import os

threshold_size = 500

for folder, subfolders, files in os.walk(os.getcwd()):
    for file in files:
        filePath = os.path.abspath(file)
        if os.path.getsize(filePath) >= threshold_size:
            print filePath, str(os.path.getsize(filePath))+"kB"

这是我目前的解决方法:

import os

threshold_size = 500

for folder, subfolders, files in os.walk(os.getcwd()):
    path = os.path.abspath(folder)
    for file in files:
        filePath = path + "\\" + file
        if os.path.getsize(filePath) >= threshold_size:
            print filePath, str(os.path.getsize(filePath))+"kB"

对于 shaktimaan,这是:

for folder, subfolders, files in os.walk(os.getcwd()):
    for file in files:
        filePath = os.path.abspath(file)
        print filePath

产生这个(大多数这些文件都在项目的子文件夹中,而不是项目本身):

C:\Python27\projects\ps4.py
C:\Python27\projects\ps4_encryption_sol.py
C:\Python27\projects\ps4_recursion_sol.py
C:\Python27\projects\words.txt
C:\Python27\projects\feedparser.py
C:\Python27\projects\feedparser.pyc
C:\Python27\projects\news_gui.py
C:\Python27\projects\news_gui.pyc
C:\Python27\projects\project_util.py
C:\Python27\projects\project_util.pyc
C:\Python27\projects\ps5.py
C:\Python27\projects\ps5.pyc
C:\Python27\projects\ps5_test.py
C:\Python27\projects\test.py
C:\Python27\projects\triggers.txt
C:\Python27\projects\ps6.py
C:\Python27\projects\ps6_pkgtest.py
C:\Python27\projects\ps6_solution.py
C:\Python27\projects\ps6_visualize.py
C:\Python27\projects\ps6_visualize.pyc
C:\Python27\projects\capitalsquiz1.txt
C:\Python27\projects\capitalsquiz2.txt
C:\Python27\projects\capitalsquiz3.txt
C:\Python27\projects\capitalsquiz4.txt
C:\Python27\projects\capitalsquiz5.txt
C:\Python27\projects\capitalsquiz_answers1.txt
C:\Python27\projects\capitalsquiz_answers2.txt
C:\Python27\projects\capitalsquiz_answers3.txt
C:\Python27\projects\capitalsquiz_answers4.txt
C:\Python27\projects\capitalsquiz_answers5.txt
C:\Python27\projects\quiz.py
C:\Python27\projects\file2.txt
C:\Python27\projects\regexes.txt
C:\Python27\projects\regexsearch.py
C:\Python27\projects\testfile.txt
C:\Python27\projects\renamedates.py

最佳答案

我认为您误解了 abspath 的作用。 abspath 只是将相对路径转换为完整的绝对文件名。

例如

os.path.abspath(os.path.join(r"c:\users\anonymous\", ".."))
#produces this output : c:\users

没有任何其他信息,abspath 只能从它可以知道的唯一目录形成绝对路径,对于您的情况是当前工作目录。所以目前它正在做的是加入 os.getcwd() 和你的 file

所以你需要做的是:

for folder, subfolders, files in os.walk(os.getcwd()):
    for file in files:
        filePath = os.path.join(os.path.abspath(folder), file)

关于python - 在 os.walk() 期间找到绝对路径的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30444105/

相关文章:

python - 使用 Pandas read_csv 读取 CSV 文件时出现 parsers.pyx 错误

python - 如何从 Python 中的 RequestHandler 处理程序实例中修改 SocketServer 服务器实例中的变量?

javascript - 将文件大小与单独的文件输入一起添加

java - Spring FileInputStream 缓冲区偶尔包含不正确的字符

java - 尝试在 Java 中使用自定义比较器时出错

python - Sqlalchemy 继承 - 删除子行而不删除父行?

python - 最后一次出现正回溯

python - 如何解析来自 Python 请求的 JSON 响应?

python : extract parameters created with argparse

django - 使用 Django 配置 mongoDB