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 statementimport 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(’.’)
Falseos.path.isabs(os.path.abspath(’.’))
Trueos.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’)
Trueos.path.exists(‘C:\else’)
Falseos.path.isfile(‘D:\Datawhale\python办公自动化\python课程画图.pptx’)
Falseos.path.isfile(‘D:\Datawhale\datawhae–python自动化’)
Falseos.path.isdir(‘D:\Datawhale\datawhae–python自动化’)
Trueos.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自动化’)
0os.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 statementtotalSize = 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 statementtotalSize = 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 statementhelloFile = 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)
0for 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 sequencefor 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’)
13baconFile.close()
baconFile = open(‘bacon.txt’,‘a’)
baconFile.write(‘Bacon is not a vegetable.’)
25baconFile.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’)
83fileObj.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