python的txt、csv、ini、xml、excel文件相关操作
-
函数,一个用于专门实现某个功能的代码块(可重用)
-
内置函数 len、bin、oct、hex 等
-
自定义函数
def send_email():
pass
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}
]
for index in range(len(goods)):
item = goods[index]
print(index + 1, item['name'], item['price'])
send_email()
while True:
num = input("请输入要选择的商品序号(Q/q):")
if num.upper() == "Q":
break
if not num.isdecimal():
print("用输入的格式错误")
break
num = int(num)
send_email()
if num > 4 or num < 0:
print("范围选择错误")
break
target_index = num - 1
choice_item = goods[target_index]
print(choice_item["name"], choice_item['price'])
send_email()
-
模块,集成了很多功能的函数集合。
1. 文件操作
1.1 读文件
-
读文本文件
file_object=open("file/info.txt",mode="rb")
data = file_object.read()
file_object.close()
print(data)
text = data.decode("utf-8")
print(text)
file_object = open('file/info.txt', mode='rt', encoding="utf-8")
data = file_object.read()
file_object.close()
print(data)
-
读文件 file_object = open("file/jt.jpg", mode="rb")
date = file_object.read()
file_object.close()
print(date)
注意事项
1.2 写路径
-
写文本文件
file_object = open("file/n2.txt", mode="wb")
file_object.write("达莱".encode("utf-8"))
file_object.close()
file_object.close()
file_object=open("file/n2.txt",mode="wt",encoding="utf-8")
file_object.write("查苏娜")
file_object.close()
基础案例
-
高级案例
```python
import requests
res = requests.get(url="网址")
print(res)
```
-
网上下载文本,写入文件
- requests模块
- requests.get(url地址)
- headers
- write(res.content)写文本
import requests
res = requests.get(
url="https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start=20",
headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
)
file_object = open('files/log1.txt', mode='wb')
file_object.write(res.content)
file_object.close()
file_object = open('files/log1.txt', mode='wb')
file_object.write(res.content)
file_object.close()
import requests
res = requests.get(
url="https://hbimg.huabanimg.com/c7e1461e4b15735fbe625c4dc85bd19904d96daf6de9fb-tosv1r_fw1200",
headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
)
file_object = open('files/美女.png', mode='wb')
file_object.write(res.content)
file_object.close()
import requests
res = requests.get(
url="https://hbimg.huabanimg.com/c7e1461e4b15735fbe625c4dc85bd19904d96daf6de9fb-tosv1r_fw1200",
headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
)
file_object = open('files/美女.png', mode='wb')
file_object.write(res.content)
file_object.close()
注意事项:
关于文件的打开模式常见应用有:
-
只读:r 、rt 、rb (用)
-
只写:w 、wt 、wb (用)
-
只写:x 、xt 、xb
-
只写:a 、at 、ab 【尾部追加】(用)
-
读写 -
r+、rt+、rb+,默认光标位置:起始位置
file_object = open('file/info.txt', mode='rt+',encoding="utf-8")
data = file_object.read()
print(data)
file_object.write("你好")
file_object.close()
file_object = open('file/info.txt', mode='rt+',encoding="utf-8")
-
w+、wt+、wb+,默认光标位置:起始位置(清空文件) -
.seek(移动光标位置) file_object = open('file/info.txt', mode='wt+', encoding="utf-8")
-
x+、xt+、xb+,默认光标位置:起始位置(新文件) -
a+、at+、ab+,默认光标位置:末尾 file_object = open('file/info.txt', mode='at+',encoding="utf-8")
file_object.write("达莱")
file_object.seek(0)
data = file_object.read()
print(data)
file_object.close()
多用户注册案例 file_object = open('file/account.txt', mode='a')
while True:
user = input("用户名:")
if user.upper() == "Q":
break
pwd = input("密码:")
data = "{}-{}\n".format(user, pwd)
file_object.write(data)
file_object.close()
1.4 常见功能
1.5 上下文管理 常用的open对文件进行操作时,每次都要打开和关闭文件,比较繁琐且容易忘记关闭文件。 以后再进行文件操作时,推荐使用with上下文管理,它可以自动实现关闭文件。 with open("xxxx.txt", mode='rb') as file_object: data = file_object.read() print(data)
在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即: with open("xxxx.txt", mode='rb') as f1, open("xxxx.txt", mode='rb') as f2: pass
2. csv格式文件
-
逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。 对于这种格式的数据,我们需要利用open函数来读取文件并根据逗号分隔的特点来进行处理。 股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今SH601778,N晶科,6.29,+1.92,-43.94%,+43.94%SH688566,吉贝尔,52.66,+6.96,+15.23%,+122.29%...
ID,用户名,头像26044585,Hush,https://hbimg.huabanimg.com/51d46dc32abe7ac7f83b94c67bb88cacc46869954f478-aP4Q3V19318369,柒十一,https://hbimg.huabanimg.com/703fdb063bdc37b11033ef794f9b3a7adfa01fd21a6d1-wTFbnO15529690,Law344,https://hbimg.huabanimg.com/b438d8c61ed2abf50ca94e00f257ca7a223e3b364b471-xrzoQd18311394,Jennah·,https://hbimg.huabanimg.com/4edba1ed6a71797f52355aa1de5af961b85bf824cb71-px1nZz
import os
import requests
with open('files/mv.csv', mode='r', encoding='utf-8') as file_object:
file_object.readline()
for line in file_object:
user_id, username, url = line.strip().split(',')
print(username, url)
res = requests.get(
url=url,
headers={
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
)
if not os.path.exists("images"):
os.makedirs("images")
with open("images/{}.png".format(username), mode='wb') as img_object:
img_object.write(res.content)
3.ini格式文件
-
ini文件是Initialization File的缩写,平时用于存储软件的的配置文件。例如:MySQL数据库的配置文件。 [mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socklog-bin=py-mysql-bincharacter-set-server=utf8collation-server=utf8_general_cilog-error=/var/log/mysqld.log# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid[client]default-character-set=utf8
-
这种格式是可以直接使用open来出来,考虑到自己处理比较麻烦,所以Python为我们提供了更为方便的方式。 -
import configparser(模块) import configparser
config = configparser.ConfigParser()
config.read('files/my.ini', encoding='utf-8')
"""
result = config.sections()
print(result) # ['mysqld', 'mysqld_safe', 'client']
"""
"""
result = config.items("mysqld_safe")
print(result) # [('log-error', '/var/log/mariadb/mariadb.log'), ('pid-file', '/var/run/mariadb/mariadb.pid')]
for key, value in config.items("mysqld_safe"):
print(key, value)
"""
"""
result = config.get("mysqld","collation-server")
print(result)
"""
-
读取所有节点 import configparser
config = configparser.ConfigParser()
config.read('/Users/wupeiqi/PycharmProjects/luffyCourse/day09/files/my.conf', encoding='utf-8')
ret = config.sections()
print(ret)
>>输出
['mysqld', 'mysqld_safe', 'client']
-
读取节点下的键值 import configparser
config = configparser.ConfigParser()
config.read('/Users/wupeiqi/PycharmProjects/luffyCourse/day09/files/my.conf', encoding='utf-8')
item_list = config.items("mysqld_safe")
print(item_list)
>>输出
[('log-error', '/var/log/mariadb/mariadb.log'), ('pid-file', '/var/run/mariadb/mariadb.pid')]
-
读取节点下值(根据 节点+键 ) import configparserconfig = configparser.ConfigParser()config.read('/Users/wupeiqi/PycharmProjects/luffyCourse/day09/files/my.conf', encoding='utf-8')value = config.get('mysqld', 'log-bin')print(value)>>输出py-mysql-bin
-
检查、删除、添加节点 import configparserconfig = configparser.ConfigParser()config.read('/Users/wupeiqi/PycharmProjects/luffyCourse/day09/files/my.conf', encoding='utf-8')
4. XML格式文件 可扩展标记语言,是一种简单的数据存储语言,XML 被设计用来传输和存储数据。
-
存储,可用来存放配置文件,例如:java的配置文件。 -
传输,网络传输时以这种格式存在,例如:早期ajax传输的数据、soap协议等。 <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2023</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2026</year> <gdppc>59900</gdppc> <neighbor direction="N" name="Malaysia" /> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2026</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> </country></data>
注意:在Python开发中用的相对来比较少 4.1读取文件和内容
- from xml.etree import ElementTree as ET
- ET.pars(ET去打开xml文件)
- getroot()(# 获取根标签)
from xml.etree import ElementTree as ET
from xml.etree import ElementTree as ET
content = """
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
root = ET.XML(content)
print(root)
4.2 读取节点数据
from xml.etree import ElementTree as ETcontent = """<data> <country name="Liechtenstein" id="999" > <rank>2</rank> <year>2023</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Panama"> <rank>69</rank> <year>2026</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> </country></data>"""
from xml.etree import ElementTree as ETcontent = """<data> <country name="Liechtenstein"> <rank>2</rank> <year>2023</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Panama"> <rank>69</rank> <year>2026</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> </country></data>"""
from xml.etree import ElementTree as ETcontent = """<data> <country name="Liechtenstein"> <rank>2</rank> <year>2023</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Panama"> <rank>69</rank> <year>2026</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> </country></data>"""root = ET.XML(content)for child in root.iter('year'): print(child.tag, child.text)
from xml.etree import ElementTree as ET
content = """
<data>
<country name="Liechtenstein">
<rank>2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank>69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
root = ET.XML(content)
v1 = root.findall('country')
print(v1)
v2 = root.find('country').find('rank')
print(v2.text)
4.3 修改和删除节点 from xml.etree import ElementTree as ET
content = """
<data>
<country name="Liechtenstein">
<rank>2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank>69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
root = ET.XML(content)
rank = root.find('country').find('rank')
print(rank.text)
rank.text = "999"
rank.set('update', '2020-11-11')
print(rank.text, rank.attrib)
tree = ET.ElementTree(root)
tree.write("new.xml", encoding='utf-8')
root.remove( root.find('country') )
print(root.findall('country'))
tree = ET.ElementTree(root)
tree.write("newnew.xml", encoding='utf-8')
4.4 构建文档 <home>
<son name="儿1">
<grandson name="儿11"></grandson>
<grandson name="儿12"></grandson>
</son>
<son name="儿2"></son>
</home>
from xml.etree import ElementTree as ET
root = ET.Element("home")
son1 = ET.Element('son', {'name': '儿1'})
son2 = ET.Element('son', {"name": '儿2'})
grandson1 = ET.Element('grandson', {'name': '儿11'})
grandson2 = ET.Element('grandson', {'name': '儿12'})
son1.append(grandson1)
son1.append(grandson2)
root.append(son1)
root.append(son2)
tree = ET.ElementTree(root)
tree.write('oooo.xml', encoding='utf-8', short_empty_elements=False)
<famliy> <son name="儿1"> <grandson name="儿11"></grandson> <grandson name="儿12"></grandson> </son> <son name="儿2"></son></famliy>
from xml.etree import ElementTree as ET
<famliy> <son name="儿1"> <age name="儿11">孙子</age> </son> <son name="儿2"></son></famliy>
from xml.etree import ElementTree as ET
<user><![CDATA[你好呀]]</user>
from xml.etree import ElementTree as ET
root = ET.Element("user")
root.text = "<![CDATA[你好呀]]"
et = ET.ElementTree(root)
et.write("test.xml", encoding="utf-8")
案例: content = """<xml>
<ToUserName><![CDATA[gh_7f083739789a]]></ToUserName>
<FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName>
<CreateTime>1395658920</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event>
<MsgID>200163836</MsgID>
<Status><![CDATA[success]]></Status>
</xml>"""
from xml.etree import ElementTree as ET
info = {}
root = ET.XML(content)
for node in root:
info[node.tag] = node.text
print(info)
5.Excel格式文件
5.1读Excel
-
读sheet from openpyxl import load_workbookwb = load_workbook("files/p1.xlsx")
-
读sheet中单元格的数据 from openpyxl import load_workbook
wb = load_workbook("file/p1.xlsx")
sheet = wb.worksheets[0]
"""
cell = sheet.cell(1, 1)
print(cell.value)
print(cell.style)
print(cell.font)
print(cell.alignment)
"""
"""
c1 = sheet["A2"]
print(c1.value)
c2 = sheet['D4']
print(c2.value)
"""
"""
for cell in sheet[1]:
print(cell.value)
"""
"""
for row in sheet.rows:
print(row[0].value, row[1].value)
"""
"""
for col in sheet.columns:
print(col[1].value)
"""
-
读合并的单元格 from openpyxl import load_workbook
wb = load_workbook("file/p1.xlsx")
sheet = wb.worksheets[2]
c1 = sheet.cell(1, 1)
print(c1)
print(c1.value)
c2 = sheet.cell(1, 2)
print(c2)
print(c2.value)
from openpyxl import load_workbookwb = load_workbook('file/p1.xlsx')sheet = wb.worksheets[2]for row in sheet.rows: print(row)
>>> 输出结果
(<Cell 'Sheet1'.A1>, <MergedCell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>)
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>)
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>)
(<MergedCell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>)
(<Cell 'Sheet1'.A5>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.C5>)
5.1 写Excel 在Excel中想要写文件,大致要分为在:
-
原Excel文件基础上写内容。 from openpyxl import load_workbook
wb = load_workbook('files/p1.xlsx')
sheet = wb.worksheets[0]
cell = sheet.cell(1, 1)
cell.value = "新的开始"
wb.save("files/p2.xlsx")
-
新创建Excel文件写内容。 from openpyxl import workbook
wb = workbook.Workbook()
sheet = wb.worksheets[0]
cell = sheet.cell(1, 1)
cell.value = "新的开始"
wb.save("file/p3.xlsx")
-
Excel中的sheet和cell操作基本上都相同。 from openpyxl import workbook
wb = workbook.Workbook()
"""
sheet = wb.worksheets[0]
sheet.title = "数据集"
wb.save("p2.xlsx")
"""
"""
sheet = wb.create_sheet("工作计划", 0)
sheet.sheet_properties.tabColor = "1072BA"
wb.save("p2.xlsx")
"""
"""
wb.active = 0
wb.save("p2.xlsx")
"""
"""
sheet = wb.create_sheet("工作计划")
sheet.sheet_properties.tabColor = "1072BA"
new_sheet = wb.copy_worksheet(wb["Sheet"])
new_sheet.title = "新的计划"
wb.save("p2.xlsx")
"""
"""
del wb["用户列表"]
wb.save('files/p2.xlsx')
"""
from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFill
wb = load_workbook('files/p1.xlsx')
sheet = wb.worksheets[1]
"""
cell = sheet.cell(1, 1)
cell.value = "开始"
wb.save("p2.xlsx")
"""
"""
sheet["B3"] = "Alex"
wb.save("p2.xlsx")
"""
"""
cell_list = sheet["B2":"C3"]
for row in cell_list:
for cell in row:
cell.value = "新的值"
wb.save("p2.xlsx")
"""
"""
cell = sheet.cell(1, 1)
# horizontal,水平方向对齐方式:"general", "left", "center", "right", "fill", "justify", "centerContinuous", "distributed"
# vertical,垂直方向对齐方式:"top", "center", "bottom", "justify", "distributed"
# text_rotation,旋转角度。
# wrap_text,是否自动换行。
cell.alignment = Alignment(horizontal='center', vertical='distributed', text_rotation=45, wrap_text=True)
wb.save("p2.xlsx")
"""
"""
cell = sheet.cell(9, 2)
cell.border = Border(
top=Side(style="thin", color="FFB6C1"),
bottom=Side(style="dashed", color="FFB6C1"),
left=Side(style="dashed", color="FFB6C1"),
right=Side(style="dashed", color="9932CC"),
diagonal=Side(style="thin", color="483D8B"), # 对角线
diagonalUp=True, # 左下 ~ 右上
diagonalDown=True # 左上 ~ 右下
)
wb.save("p2.xlsx")
"""
"""
cell = sheet.cell(5, 1)
cell.font = Font(name="微软雅黑", size=45, color="ff0000", underline="single")
wb.save("p2.xlsx")
"""
"""
cell = sheet.cell(5, 3)
cell.fill = PatternFill("solid", fgColor="99ccff")
wb.save("p2.xlsx")
"""
"""
cell = sheet.cell(5, 5)
cell.fill = GradientFill("linear", stop=("FFFFFF", "99ccff", "000000"))
wb.save("p2.xlsx")
"""
"""
sheet.row_dimensions[1].height = 50
sheet.column_dimensions["E"].width = 100
wb.save("p2.xlsx")
"""
"""
sheet.merge_cells("B2:D8")
sheet.merge_cells(start_row=15, start_column=3, end_row=18, end_column=8)
wb.save("p2.xlsx")
"""
"""
sheet.unmerge_cells("B2:D8")
wb.save("p2.xlsx")
"""
"""
sheet = wb.worksheets[3]
sheet["D1"] = "合计"
sheet["D2"] = "=B2*C2"
wb.save("p2.xlsx")
"""
"""
sheet = wb.worksheets[3]
sheet["D3"] = "=SUM(B3,C3)"
wb.save("p2.xlsx")
"""
"""
# idx,要删除的索引位置
# amount,从索引位置开始要删除的个数(默认为1)
sheet.delete_rows(idx=1, amount=20)
sheet.delete_cols(idx=1, amount=3)
wb.save("p2.xlsx")
"""
"""
sheet.insert_rows(idx=5, amount=10)
sheet.insert_cols(idx=3, amount=2)
wb.save("p2.xlsx")
"""
"""
sheet = wb["Sheet"]
cell_range = sheet['A1:C2']
for row in cell_range:
for cell in row:
cell.value = "xx"
for row in sheet.iter_rows(min_row=5, min_col=1, max_col=7, max_row=10):
for cell in row:
cell.value = "oo"
wb.save("p2.xlsx")
"""
"""
# 将H2:J10范围的数据,向右移动15个位置、向上移动1个位置
sheet.move_range("H2:J10",rows=1, cols=15)
wb.save("p2.xlsx")
"""
"""
sheet = wb.worksheets[3]
sheet["D1"] = "合计"
sheet["D2"] = "=B2*C2"
sheet["D3"] = "=SUM(B3,C3)"
sheet.move_range("B1:D3",cols=10, translate=True) # 自动翻译公式
wb.save("p2.xlsx")
"""
"""
sheet.print_area = "A1:D200"
wb.save("p2.xlsx")
"""
"""
sheet.print_title_cols = "A:D"
sheet.print_title_rows = "1:3"
wb.save("p2.xlsx")
6. 压缩文件
- 基于Python内置的shutil模块可以实现对压缩文件的操作。
import shutil
7.路径相关 7.1转译 windows路径使用的是\,linux路径使用的是/。 特别的,在windows系统中如果有这样的一个路径 D:\nxxx\txxx\x1 ,程序会报错。因为在路径中存在特殊符 \n (换行符)和\t (制表符),Python解释器无法自动区分。 所以,在windows中编写路径时,一般有两种方式:
- 加转义符,例如:
"D:\\nxxx\\txxx\\x1" - 路径前加r,例如:
r"D:\\nxxx\\txxx\\x1" 7.2 程序当前路径 项目中如果使用了相对路径,那么一定要注意当前所在的位置。 例如:在/Users/dalai/PycharmProjects/CodeRepository/ 路径下编写 demo.py 文件 with open("a1.txt", mode='w', encoding='utf-8') as f:
f.write("你好呀")
用以下两种方式去运行:
- 方式1,文件会创建在
/Users/dalai/PycharmProjects/CodeRepository/ 目录下。 cd /Users/dalai/PycharmProjects/CodeRepository/
python demo.py
- 方式2,文件会创建在
/Users/dalai 目录下。 cd /Users/dalai
python /Users/dalai/PycharmProjects/CodeRepository/demo.py
import os
"""
# 1.获取当前运行的py脚本所在路径
abs = os.path.abspath(__file__)
print(abs) # /Users/wupeiqi/PycharmProjects/luffyCourse/day09/20.路径相关.py
path = os.path.dirname(abs)
print(path) # /Users/wupeiqi/PycharmProjects/luffyCourse/day09
"""
base_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(base_dir, 'files', 'info.txt')
print(file_path)
if os.path.exists(file_path):
file_object = open(file_path, mode='r', encoding='utf-8')
data = file_object.read()
file_object.close()
print(data)
else:
print('文件路径不存在')
7.3文件和路径相关 import shutil
import os
"""
abs_path = os.path.abspath(__file__)
print(abs_path)
"""
"""
base_path = os.path.dirname( os.path.dirname(路径) )
print(base_path)
"""
"""
p1 = os.path.join(base_path, 'xx')
print(p1)
p2 = os.path.join(base_path, 'xx', 'oo', 'a1.png')
print(p2)
"""
"""
exists = os.path.exists(p1)
print(exists)
"""
"""
os.makedirs(路径)
"""
"""
path = os.path.join(base_path, 'xx', 'oo', 'uuuu')
if not os.path.exists(path):
os.makedirs(path)
"""
"""
file_path = os.path.join(base_path, 'xx', 'oo', 'uuuu.png')
is_dir = os.path.isdir(file_path)
print(is_dir) # False
folder_path = os.path.join(base_path, 'xx', 'oo', 'uuuu')
is_dir = os.path.isdir(folder_path)
print(is_dir) # True
"""
"""
os.remove("文件路径")
"""
"""
path = os.path.join(base_path, 'xx')
shutil.rmtree(path)
"""
"""
shutil.copytree("/Users/wupeiqi/Desktop/图/csdn/","/Users/wupeiqi/PycharmProjects/CodeRepository/files")
"""
"""
shutil.copy("/Users/wupeiqi/Desktop/图/csdn/WX20201123-112406@2x.png","/Users/wupeiqi/PycharmProjects/CodeRepository/")
shutil.copy("/Users/wupeiqi/Desktop/图/csdn/WX20201123-112406@2x.png","/Users/wupeiqi/PycharmProjects/CodeRepository/x.png")
"""
"""
shutil.move("/Users/wupeiqi/PycharmProjects/CodeRepository/x.png","/Users/wupeiqi/PycharmProjects/CodeRepository/xxxx.png")
shutil.move("/Users/wupeiqi/PycharmProjects/CodeRepository/files","/Users/wupeiqi/PycharmProjects/CodeRepository/images")
"""
|