目录
1. 输入 input()、sys.stdin.readline()
2. 输出 print()
3. 遍历文件夹 os.walk()、os.path.xxx()
- # 读入一个数字
- n = int(input())
-
- # 读入两个或以上数字
- m, n = map(int, input().split())
-
- # 一行数据,以空格分隔
- line = input()
- arr = []
- for i in line.split():
- arr.append(int(i))
- import sys
-
- # 一行一行读取 并分隔为列表
- line = sys.stdin.readline().split()
- # 取出n值
- n = int(line[0])
- # 读入矩阵(多行),以空格分隔
- arr = []
- while 1:
- s = input()
- if s != "":
- arr.append(list(map(int, s.split())))
- else:
- break
-
- # 读入矩阵(多行),输入的数组中带有中括号和逗号
- arr = []
- while 1:
- s = input()
- if s != "":
- arr.append(list(map(int, s.replace("],", "").replace(" ", "").replace("[", "").replace("]", "").split(","))))
- else:
- break
输出列表,以空格分隔:
- # 输出列表,以空格分隔
- for i in arr:
- print(i,end=" ")
-
- # 输出列表,以空格分隔(严格版)
- for i in range(n):
- if i != n-1:
- print(ret[i],end=" ")
- else:
- print(ret[i])
os.walk()自动递归找出所有文件名
- for filepath,dirnames,filenames in os.walk(r'xxxxx'):
- for filename in filenames:
- print (os.path.join(filepath,filename))
遍历文件夹下的所有文件,获取文件名列表
- import os
- def allfiles(path, all_files=[]):
- # 当输入路径存在时才获取路径下文件列表
- if os.path.exists(path):
- files = os.listdir(path)
-
- # 遍历files列表
- for file in files:
- # 判断是否为文件夹
- if os.path.isdir(os.path.join(path, file)):
- # 递归调用
- allfiles(os.path.join(path, file),all_files)
- else:
- all_files.append(os.path.join(path, file))
- return all_files
获取路径下包含指定名字的文件路径(path指定路径,name要找的文件名)
- import os
- def allfiles(path, name, all_files=[]):
- # 当输入路径存在时才获取路径下文件列表
- if os.path.exists(path):
- files = os.listdir(path)
-
- # 遍历files列表
- for file in files:
- # 判断是否为文件夹
- if os.path.isdir(os.path.join(path, file)):
- # 递归调用
- allfiles(os.path.join(path, file), name, all_files)
- else:
- if name in file:
- all_files.append(os.path.join(path, file))
- return all_files
| os.walk(folder) | 返回 filepath,dirnames,filenames三元组 |
| os.path.exists(path) | 判断路径是否存在 |
| os.path.isdir(xxx) | 判断是否为路径 |
| os.path.isfile(xxx) | 判断是否为文件 |
| os.listdir(path) | 返回路径下所有文件夹和文件名 |
| os.getcwd() | 获得当前执行路径 |
| os.path.join(path, name) | 获得绝对路径,把路径和文件名拼接起来 |