鱼C论坛

 找回密码
 立即注册
查看: 1446|回复: 5

零基础学习Python之30讲课后习题第二题

[复制链接]
发表于 2018-4-19 13:58:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import os
path =  input('input file absolutely path')
all_files = os.listdir(path) # 使用os.curdir表示当前目录更标准
file_dict = dict()

for each_file in all_files:
    print(each_file)
    if os.path.isfile(each_file):
        file_size = os.path.getsize(each_file)
        file_dict[each_file] = file_size

print(len(file_dict))

for each in file_dict.items():
    print('%s【%dBytes】' % (each[0], each[1]))
输出:
input file absolutely pathE:
$RECYCLE.BIN
360Downloads
360安全浏览器下载
AAA.xls
BaiduNetdiskDownload
DGSetup_1316E.exe
Docu
lean.jpg
meitu
office.exe
Program Files (x86)
qt-win-opensource-4.8.5-vs2010.exe
RfTestUser.zip
SUTLR_v3.2.3.zip
System Volume Information
vcredist.bmp
VC_RED.cab
VC_RED.MSI
waveform
web
备份
安装包
工作
校准
迅雷下载
0

为何没有读出文件的大小?我一行一行的在IDLE里敲又可以
>>> os.path.isfile('lean.jpg')
               
True
>>> os.path.getsize('lean.jpg')
               
14217
>>> file_dict = dict()
               
>>> file_dict
               
{}
>>> file_dict['lean.jpg'] = os.path.getsize('lean.jpg')
               
>>> file_dict
               
{'lean.jpg': 14217}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 14:09:02 | 显示全部楼层
你在if里面加一句print试试有没有
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-19 14:12:49 | 显示全部楼层
BngThea 发表于 2018-4-19 14:09
你在if里面加一句print试试有没有

import os
path =  input('input file absolutely path')
all_files = os.listdir(path) # 使用os.curdir表示当前目录更标准
file_dict = dict()

for each_file in all_files:
    print(each_file)
    if os.path.isfile(each_file):
        file_size = os.path.getsize(each_file)
        file_dict[each_file] = file_size
        print(each_file,':',file_size)

print(len(file_dict))

for each in file_dict.items():
    print('%s【%dBytes】' % (each[0], each[1]))
也没有打印
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-19 14:16:30 | 显示全部楼层
import os
path =  input('input file absolutely path')
os.chdir(path)
all_files = os.listdir(os.curdir) # 使用os.curdir表示当前目录更标准
file_dict = dict()

for each_file in all_files:
    print(each_file)
    if os.path.isfile(each_file):
        file_size = os.path.getsize(each_file)
        file_dict[each_file] = file_size
        print(each_file,':',file_size)

print(len(file_dict))

for each in file_dict.items():
    print('%s【%dBytes】' % (each[0], each[1]))

改成这样后就可以了,但是不知道为什么需要这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 15:31:04 | 显示全部楼层
本帖最后由 thexiosi 于 2018-4-19 15:32 编辑
AIlaopo 发表于 2018-4-19 14:16
import os
path =  input('input file absolutely path')
os.chdir(path)


hi 原因如下

之前遗失了 os.chdir(path)这行代码,导致统计目录为.py文件所在目录,而不是 用户输入目录path。加上os.chdir(path)后,将当前目录 从.py文件所在目录切换为用户输入目录,使得统计目录与预期一致
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 15:39:33 | 显示全部楼层
本帖最后由 thexiosi 于 2018-4-19 15:44 编辑
thexiosi 发表于 2018-4-19 15:31
hi 原因如下

之前遗失了 os.chdir(path)这行代码,导致统计目录为.py文件所在目录,而不是 用户输入 ...


抱歉,我说的应该不对,之前的方法应该也可以切换当前工作目录
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-29 13:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表