2021-06-17python文件自动化处理1

本文演示了Python中常见的文件操作方法,包括路径处理、文件读写、目录管理及使用shelve模块进行数据持久化存储。通过实例展示了如何获取文件路径、检查文件存在性、读取文件内容等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license()” for more information.

import os
os.path.join(‘Datawhale’,‘docu’)
SyntaxError: multiple statements found while compiling a single statement

import os
os.path.join(‘Datawhale’,‘docu’)
‘Datawhale\docu’

os.getcwd()
‘D:\’

os.chdir(‘D:\Datawhale\python办公自动化’)
Traceback (most recent call last):
File “<pyshell#4>”, line 1, in
os.chdir(‘D:\Datawhale\python办公自动化’)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: ‘D:\Datawhale\python办公自动化’

os.chdir(‘D:\Datawhale\datawhae–python自动化’)
os.getcwd()
‘D:\Datawhale\datawhae–python自动化’

os.path.abspath(’.’)
‘D:\Datawhale\datawhae–python自动化’

os.path.isabs(’.’)
False

os.path.isabs(os.path.abspath(’.’))
True

os.path.relpath(‘D:\Datawhale\datawhae–python自动化’,‘D:\’)
‘Datawhale\datawhae–python自动化’

path = ‘D:\Datawhale\python办公自动化\python课程画图.pptx’
os.path.dirname(path)
‘D:\Datawhale\python办公自动化’

os.path.basename(path)
‘python课程画图.pptx’

caFilePath = ‘D:\Datawhale\python办公自动化\python课程画图.pptx’
os.path.split(caFilePath)
(‘D:\Datawhale\python办公自动化’, ‘python课程画图.pptx’)

(os.path.dirname(caFilePath),os.path.basename(caFilePath))
(‘D:\Datawhale\python办公自动化’, ‘python课程画图.pptx’)

caFilePath.split(os.path.sep)
[‘D:’, ‘Datawhale’, ‘python办公自动化’, ‘python课程画图.pptx’]

os.path.exists(‘C:\Windows’)
True

os.path.exists(‘C:\else’)
False

os.path.isfile(‘D:\Datawhale\python办公自动化\python课程画图.pptx’)
False

os.path.isfile(‘D:\Datawhale\datawhae–python自动化’)
False

os.path.isdir(‘D:\Datawhale\datawhae–python自动化’)
True

os.makedirs(‘D:\Datawhale\practice’)
os.path.getsize(‘D:\Datawhale\python办公自动化\python课程画图.pptx’)
Traceback (most recent call last):
File “<pyshell#24>”, line 1, in
os.path.getsize(‘D:\Datawhale\python办公自动化\python课程画图.pptx’)
File “D:\lib\genericpath.py”, line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: ‘D:\Datawhale\python办公自动化\python课程画图.pptx’

os.path.getsize(‘D:\Datawhale\datawhae–python自动化’)
0

os.listdir(‘D:\Datawhale\datawhae–python自动化’)
[‘OfficeAutomation’, ‘OfficeAutomation.rar’]

totalSize = 0
for filename in os.listdir(‘D:\Datawhale\datawhae–python自动化’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\datawhae–python自动化’,filename))
print(totalSize)
SyntaxError: multiple statements found while compiling a single statement

totalSize = 0
for filename in os.listdir(‘D:\Datawhale\datawhae–python自动化’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\datawhae–python自动化’,filename))
print(totalSize)
SyntaxError: multiple statements found while compiling a single statement

totalSize = 0
for filename in os.listdir(‘D:\Datawhale\datawhae–python自动化\OfficeAutomation’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\datawhae–python自动化\OfficeAutomation’,filename))
print(totalSize)
SyntaxError: multiple statements found while compiling a single statement

helloFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
print(helloFile)
Traceback (most recent call last):
File “<pyshell#30>”, line 1, in
helloFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
FileNotFoundError: [Errno 2] No such file or directory: ‘D:\Datawhale\python办公自动化\hello.txt’

helloFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
Traceback (most recent call last):
File “<pyshell#31>”, line 1, in
helloFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
FileNotFoundError: [Errno 2] No such file or directory: ‘D:\Datawhale\python办公自动化\hello.txt’

os.makedirs(‘D:\Datawhale\datawhae–python自动化\hello.txt’)
helloFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
Traceback (most recent call last):
File “<pyshell#33>”, line 1, in
helloFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
FileNotFoundError: [Errno 2] No such file or directory: ‘D:\Datawhale\python办公自动化\hello.txt’

helloFile = open(‘D:\Datawhale\datawhae–python自动化\hello.txt’)
Traceback (most recent call last):
File “<pyshell#34>”, line 1, in
helloFile = open(‘D:\Datawhale\datawhae–python自动化\hello.txt’)
PermissionError: [Errno 13] Permission denied: ‘D:\Datawhale\datawhae–python自动化\hello.txt’

helloFile = open(‘D:\Datawhale\datawhae–python自动化\hello.txt’)
totalSize = 0
for filename in os.listdir(‘D:\Datawhale\python办公自动化’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\python办公自动化’,filename))

Traceback (most recent call last):
File “<pyshell#39>”, line 1, in
for filename in os.listdir(‘D:\Datawhale\python办公自动化’):
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: ‘D:\Datawhale\python办公自动化’

print(totalSize)
0

for filename in os.listdir(‘D:\Datawhale\python办公自动化’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\python办公自动化’,filename))
print(totalSize)

Traceback (most recent call last):
File “<pyshell#44>”, line 1, in
for filename in os.listdir(‘D:\Datawhale\python办公自动化’):
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: ‘D:\Datawhale\python办公自动化’

for filename in os.listdir(‘D:\Datawhale\datawhae–python自动化\python办公自动化’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\datawhae–python自动化’,filename))
print(totalSize)

SyntaxError: invalid escape sequence \d

for filename in os.listdir(‘D:\Datawhale\datawhae–python自动化\python办公自动化’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\datawhae–python自动化’,filename))
print(totalSize)

SyntaxError: invalid escape sequence \d

print(helloFile)
<_io.TextIOWrapper name=‘D:\Datawhale\datawhae–python自动化\hello.txt’ mode=‘r’ encoding=‘cp936’>

helloContent = helloFile.read()
helloContent
‘’

helloContent
‘’

helloContent = helloFile.read()
Traceback (most recent call last):
File “<pyshell#51>”, line 1, in
helloContent = helloFile.read()
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 8: illegal multibyte sequence

for filename in os.listdir(‘D:\Datawhale\datawhae–python自动化’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\datawhae–python自动化’,filename))
print(totalSize)

SyntaxError: EOL while scanning string literal

totalSize = 0
for filename in os.listdir(‘D:\Datawhale\datawhae–python自动化’):
totalSize = totalSize + os.path.getsize(os.path.join(‘D:\Datawhale\datawhae–python自动化’,filename))
print(totalSize)

42
4138
601279

helloFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
print(helloFile)
<_io.TextIOWrapper name=‘D:\Datawhale\python办公自动化\hello.txt’ mode=‘r’ encoding=‘cp936’>

helloContent = helloFile.read()
helloContent
‘’

helloContent = helloFile.read()
helloContent
‘hello i love you’

sonnetFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
sonnetFile.readlines()
[‘hello i love you’]

sonnetFile = open(‘D:\Datawhale\python办公自动化\hello.txt’)
sonnetFile.readlines()
[‘hello i love you\n’, ‘i know ,you love me too’]

helloContent = helloFile.read()
helloContent
‘\ni know ,you love me too’

baconFile = open(‘bacon.txt’,‘w’)
baconFile.write(‘Hello world!\n’)
13

baconFile.close()
baconFile = open(‘bacon.txt’,‘a’)
baconFile.write(‘Bacon is not a vegetable.’)
25

baconFile.close()
baconFile
<_io.TextIOWrapper name=‘bacon.txt’ mode=‘a’ encoding=‘cp936’>

baconFile = open(‘bacon.txt’)
content = baconFile.read()
baconFile.close()
print(content)
Hello world!
Bacon is not a vegetable.

import shelve
shelfFile = shelve.open(‘mydata’)
cats = [‘Zonphie’,‘Pooka’,‘Simon’]
shelfFile[‘cats’] = cats
shelfFile.close()
shelfFile = shelve.open(‘mydata’)
type(shelfFile)
<class ‘shelve.DbfilenameShelf’>

shelve.DbfilenameShelf
<class ‘shelve.DbfilenameShelf’>

shelfFile[‘cats’]
[‘Zonphie’, ‘Pooka’, ‘Simon’]

shelfFile.close()
shelfFile = shelve.open(‘mydata’)
list(shelfFile.keys())
[‘cats’]

list(shelfFile.values())
[[‘Zonphie’, ‘Pooka’, ‘Simon’]]

shelfFile.close()
import pprint
cats = [{‘name’:‘Zophie’,‘desc’:‘chubby’},{‘name’:‘Pooka’,‘desc’:‘fluffy’}]
pprint.pformat(cats)
“[{‘desc’: ‘chubby’, ‘name’: ‘Zophie’}, {‘desc’: ‘fluffy’, ‘name’: ‘Pooka’}]”

fileObj = open(‘myCats.py’,‘w’)
fileObj.write(‘cats = ‘+pprint.pformat(cats)+’\n’)
83

fileObj.close()
import myCats
myCats.cats
[{‘desc’: ‘chubby’, ‘name’: ‘Zophie’}, {‘desc’: ‘fluffy’, ‘name’: ‘Pooka’}]

myCats.cats[0]
{‘desc’: ‘chubby’, ‘name’: ‘Zophie’}

myCats.cats[0][‘name’]
‘Zophie’

import shutil

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值