在os.walk()期间找到绝对路径的更好方法? [英] Better way to find absolute paths during os.walk()?

查看:171
本文介绍了在os.walk()期间找到绝对路径的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am practicing with the os module and more specifically os.walk(). I am wondering if there is an easier/more efficient way to find the actual path to a file considering this produces a path that suggests the file is in the original folder when os.walk() is first ran:

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"

这是我当前的解决方法:

This is my current workaround:

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 shaktimaan, this:

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

生成此文件(大多数文件位于项目的子文件夹中,而不是项目本身):

produces this(most of these files are in a subfolder of projects, not projects itself):

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只是将相对路径转换为完整的绝对文件名.

I think there you mistook what abspath does. abspath just convert a relative path to a complete absolute filename.

例如

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

没有任何其他信息,abspath只能从它可以知道的唯一目录(对于您的情况是当前工作目录)形成一个绝对路径.因此,当前正在执行的操作是将os.getcwd()和您的file

Without any other information, abspath can only form an absolute path from the only directory it can know about, for your case the current working directory. So currently what it is doing is it joins os.getcwd() and your file

所以您要做的是:

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

这篇关于在os.walk()期间找到绝对路径的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆