python打卡day24@浙大疏锦行

知识点回顾:

  1. 元组
  2. 可迭代对象
  3. os模块

作业:对自己电脑的不同文件夹利用今天学到的知识操作下,理解下os路径。

一、元组

tuple1=(1,2,3)
tuple2=('a','b','c')
tuple3=(1,'hello',3.14,[4,5]) 
tuple4=1,4,'hello'
print(tuple1)   
print(tuple2)  
print(tuple3)   
print(tuple4)   
print(type(tuple4)) 
 
 
my_tuple = ('P', 'y', 't', 'h', 'o', 'n')
print(my_tuple[0])    
print(my_tuple[2])    
print(my_tuple[-1])   
print(my_tuple[1:4])  
print(my_tuple[:3])   
print(my_tuple[3:])   
print(my_tuple[::2])  
print(len(my_tuple))  

二、可迭代对象

print("迭代列表:")
my_list = [1, 2, 3, 4, 5]
for item in my_list:
    print(item)
 
print("迭代元组:")
my_tuple = ('a', 'b', 'c')
for item in my_tuple:
    print(item)
 
print("迭代字符串:")
my_string = "hello"
for char in my_string:
    print(char)
 
print("迭代 range:")
for number in range(5):  
    print(number)
 
print("迭代集合:")
my_set = {3, 1, 4, 1, 5, 9}
for item in my_set:
    print(item)
 
print("迭代字典 (默认迭代键):")
my_dict = {'name': 'Alice', 'age': 30, 'city': 'Singapore'}
for key in my_dict:
    print(key)
 
print("迭代字典的值:")
for value in my_dict.values():
    print(value)
    
print("迭代字典的键值对:")
for key, value in my_dict.items(): 
    print(f"Key: {key}, Value: {value}")

三、os模块

①当前工作目录

import os
 
print("当前工作目录:", os.getcwd())
print("当前目录下的文件列表:", os.listdir())
 
path_a=r'C:\Users\YourUsername\Documents'
path_b='MyProjectData'
file='results.csv'
 
file_path=os.path.join(path_a,path_b,file)
print(file_path)    

②环境变量

print(os.environ)
for variable_name,variable_value in os.environ.items():
    print(f"{variable_name}:{variable_value}")
print(f"\n--- 总共检测到 {len(os.environ)} 个环境变量 ---")

③目录树

import os
 
start_directory = r"d:\python_60"
 
print(f"--- 开始遍历目录: {start_directory} ---")
 
for dirpath, dirnames, filenames in os.walk(start_directory):
    print(f"  当前访问目录 (dirpath): {dirpath}")
    print(f"  子目录列表 (dirnames): {dirnames}")
    print(f"  文件列表 (filenames): {filenames}")
    print("    文件完整路径:")
    for filename in filenames:
        full_path = os.path.join(dirpath, filename)
        print(f"      - {full_path}")
    print("\n")

你可能感兴趣的:(python打卡60天行动,python,开发语言)