admin 管理员组

文章数量: 887007

python访问手机存储文件夹

管理电脑文件及文件夹

1、输出目录下所有文件及文件夹

(2)获取当前python程序运行路径

importosprint(os.getcwd())

print(os.path.join('xuexi python','01变量.py'))

(3)让python自动处理路径连接

(4)绝对路径与相对路径

(5)列出当前程序文件夹(及指定文件夹)下的所有文件和文件夹

importosprint(os.getcwd())print(os.path.join('xuexi python','01变量.py'))print(os.listdir()) #列出当前程序文件夹下的所有文件和文件夹(列表形式)

for item in os.listdir(): #列出当前程序文件夹下的所有文件和文件夹

print(item,os.path.isdir(item))print(os.listdir('E:\Youku Files')) #列出某个指定文件夹下的所有文件和文件夹(列表形式),英文路径

for file inos.scandir():print(file.name,file.path,file.is_dir())for file in os.scandir('E:\Youku Files'):print((file.name,file.path,file.is_dir()))

2.遍历、搜索文件及查询文件信息

(1)遍历文件夹

importosfor dirpath, dirnames, files in os.walk('E:\Youku Files'):print(f'发现文件夹:{dirpath}')print(files)print('666good'.startswith('6'))print('good.txt'.endswith('t'))

(2)搜索、匹配文件名称

importglobprint(glob.glob('1*.py'))print(glob.glob('?1*.py'))print(glob.glob('0[1-8]*.py'))print(glob.glob('[1,0]*.py'))print(glob.glob('[!1,6]*.py'))print(glob.glob('*.txt', recursive=True))importfnmatchprint(fnmatch.fnmatch('12函数使用.py','*使用.py'))print(fnmatch.fnmatch('12函数使用.py','[0-9]?函数??.py'))

(3)查询文件信息

importosimporttimeimportdatetimefor file inos.scandir():print(file.name,file.stat(),file.stat().st_size/1024,time.ctime(file.stat().st_mtime))

file_time=file.stat().st_mtimeprint(datetime.datetime.fromtimestamp(file_time))

print(os.stat('01变量.py'))

importdatetime

normal_time= datetime.datetime.fromtimestamp(1498912896)print(normal_time,normal_time.year,normal_time.month,normal_time.day)

3、写入和读取文件;创建临时文件及文件夹

(1)读取文件

f = open('file.txt','r',encoding= 'utf-8')

text=f.readlines()print(text)

f.close

with open('D:\Desktop\快捷键.txt','r',encoding='ansi') as fi:

text_fi = fi.readlines()

print(text_fi)

(2)写入文件的两种模式(覆盖写 和 接着写)

with open('file.txt','w',encoding= 'utf-8') as f:

text= 'a\nb\nc\n'f.write(text)

f.write('d')

with open('file.txt','a',encoding= 'utf-8') as f:

f.write('\n')

f.write('good')

(3)创建临时文件和存储数据

from tempfile importTemporaryFile

f= TemporaryFile('w+')

f.write("good!good!")

f.seek(0)

data=f.readlines()print(data)

f.close

with TemporaryFile("w+") as f:

f.write("6jl very good")

f.seek(0)

data=f.readlines()print(data)

(4)创建临时文件夹

from tempfile importTemporaryDirectory

with TemporaryDirectory() as tmp_folder:print(f'临时文件夹已创建:{tmp_folder}')

4、批量创建、复制、移动、删除、重命名文件及文件夹

(1)创建文件夹

importosif not os.path.exists("新文件夹"):

os.mkdir('新文件夹')

importos

os.makedirs('一/二')

(2)复制文件、文件夹

importshutil

shutil.copy('file.txt','./新文件夹')

shutil.copy('file.txt','./新文件夹/new_file.txt')

shutil.copytree('一','新文件夹1')

(3)移动文件和文件夹

importshutil

shutil.move('./新文件夹/file.txt','./一/二/file2.txt')

shutil.move("./一/二/file2.txt",'新文件夹/')

shutil.move("一",'新文件夹1/')

(4)重命名文件和文件夹

importos

os.rename('新文件夹','文件夹new')

os.rename('./文件夹new/文件夹一','./文件夹new/wjj')

os.rename('./文件夹new/file2.txt','./文件夹new/file_new.txt')

(5)删除文件,删除文件夹

importos

os.remove("./文件夹new/file2.txt")importshutil

shutil.rmtree('./文件夹new')

5、创建和解压压缩包

(1)读取压缩包

importzipfile

with zipfile.ZipFile('xuexi.zip','r') as zipobj:for filename inzipobj.namelist():print(filename.encode('cp437').decode('gbk'))

# 读取压缩包内文件信息

importzipfile

with zipfile.ZipFile('xuexi.zip','r') as zipobj:for filename inzipobj.namelist():

info=zipobj.getinfo(filename)

nfilename= filename.encode('cp437').decode('gbk')print(nfilename,info.file_size,infopress_size,info)

(2)解压压缩包

importzipfile

with zipfile.ZipFile('xuexi.zip','r') as zipobj:

zipobj.extract('pyvenv.cfg','./xuexi/')

zipobj.extractall(path= './xuexi')

(3)创建压缩包

importzipfile

file_list= ['01.txt','./02.txt']

with zipfile.ZipFile('newzip.zip','w') as zipobj:for file infile_list:

zipobj.write(file)

本文标签: python访问手机存储文件夹