(1)得到脚本当前工作目录并输出。
(2)输出当前脚本目录下所有文件名和目录名。
(3) 进入temp文件夹,并通过getcwd函数获得该文件夹的完整路径并输出。
(4)输出temp目录下所有文件名和目录名。
(5) 把temp目录下的文件名和目录名分别放在两个列表对象s1和s2中。
(6)输出s1和s2。
(7)使用getsize方法获得s1中所有文件的大小并输出。
参考下
import os
# (1)得到脚本当前工作目录并输出。
print(f"当前工作目录:{os.getcwd()}")
# (2)输出当前脚本目录下所有文件名和目录名。
print(f"当前脚本目录下的所有文件和目录:{os.listdir()}")
# (3)进入temp文件夹,并通过getcwd函数获得该文件夹的完整路径并输出。
os.chdir(os.path.join(os.getcwd(), "temp"))
print(f"当前工作目录:{os.getcwd()}")
# (4)输出temp目录下所有文件名和目录名。
print(f"temp目录下的所有文件和目录:{os.listdir()}")
# (5)把temp目录下的文件名和目录名分别放在两个列表对象s1和s2中。
s1 = []
s2 = []
for name in os.listdir():
if os.path.isfile(name):
s1.append(name)
else:
s2.append(name)
# (6)输出s1和s2。
print("s1中的文件名:", s1)
print("s2中的目录名:", s2)
# (7)使用getsize方法获得s1中所有文件的大小并输出
for file_name in s1:
file_size = os.path.getsize(os.path.join(os.getcwd(), file_name))
print(f"{file_name} 的大小为 {file_size} 字节")
不知道你这个问题是否已经解决, 如果还没有解决的话: