python 基础知识学习笔记¶
1、os.listdir(path)¶
- 描述:
返回path目录下的文件名和文件夹名
- 参数:
path:输入 目录
- 示例:
[1]:
import os
c_files = os.listdir('/tmp')
print(c_files)
['fm_sg_cloud_cache.v0.filemap.sogouime_2.9.70580_00000000_501', '.keystone_install_lock', 'powerlog', 'escalatelantern.ico', 'com.sogou.inputmethod.qq', 'com.apple.launchd.uhHN3iO6E8', 'com.apple.launchd.YMiSho5zW2']
str1.split(str=”“, num=string.count(str))¶
- 描述:
Python split()通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串
- 参数:
str:分隔符,默认为所有的空字符,包括空格、换行、制表符 等
num:分割的次数
- 参考链接
http://www.runoob.com/python/att-string-split.html
- 示例:
[2]:
str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print(str.split( ));
print(str.split(' ', 1 ));
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
re.split(pattern, string, maxsplit=0, flags=0)¶
- 描述:
对字符串按照正则表达式匹配的分割
- 参数:
pattern:正则表达式
string:待分割字符
- 参考链接:
https://docs.python.org/3/library/re.html
- 示例:
[3]:
import re
a='Beautiful, is; better*than\nugly'
# 四个分隔符为:, ; * \n
x= re.split(',|; |\*|\n',a)
print(x)
['Beautiful', ' is', 'better', 'than', 'ugly']
enumerate(sequence, [start=0)¶
- 描述:
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
- 参数:
sequence : 可遍历的数据对象(如列表、元组或字符串)
start : 索引开始的值
- 参考链接:
http://www.runoob.com/python/python-func-enumerate.html
- 示例:
[6]:
for i, season in enumerate(['Spring', 'Summer', 'Fall', 'Winter'],start = 2):
print(i,season)
2 Spring
3 Summer
4 Fall
5 Winter
[2]:
ls()
驱动器 D 中的卷是 OTDisk
卷的序列号是 DE9D-0AB8
D:\jdjr\project_jdjr\blog-hwli\chapter-python 的目录
找不到文件
[3]:
pwd()
[3]:
'D:\\jdjr\\project_jdjr\\blog-hwli\\chapter-python'
[ ]: