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