使用Python怎么处理系统文件目录

  介绍

本篇文章给大家分享的是有关使用Python怎么处理系统文件目录,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Python - - - - - OS文件目录处理

import 操作系统   import 时间      #,获取当前文件的绝对路径   时间=dir_1  os.path.abspath (__file__),,,, #, D: \ workspace \ web测试\ \ Day_5-16 \ tset3.py学习         #,获取当前文件所在目录的上级路径   时间=dir_2  os.getcwd (),,,,,,,,,,,,,,,,,,,, #, D: \ workspace \ web测试\ \ Day_5-16学习   时间=dir_3_1  os.path.dirname (dir_1),,,,,,, #, D: \ workspace \ web测试\ \ Day_5-16学习   时间=dir_3_2  os.path.dirname (dir_3_1),,,,, #, D: \ workspace \ web测试\学习         #,获取当前文件所在目录的上级路径   时间=dir_4  os.path.abspath (os.path.join (os.getcwd (),“。/?),,,,,, #, D: \ workspace \ web测试\ \ Day_5-16学习   dir_5 =, os.path.abspath (os.path.join (os.getcwd (),“. ./?),,,,, #, D: \ workspace \ web测试\学习   dir_6 =, os.path.abspath (os.path.join (os.getcwd (),“. ./. ./?),, #, D: \ workspace \ web测试            #,获取当前日期   new_time_day =, time.strftime (& # 39; Y - % - % d % # 39;),,,,,, # 2021-05-21   #,拼接目录   时间=dir_image_day_pingjie  os.path.join (dir_4 new_time_day),, #, D: \ workspace \ web测试\ \ Day_5-16 \ 2021-05-21      #判断目录是否存在,不存在就创建   if  not  os.path.exists (dir_image_day_pingjie):   ,,,#创建文件   ,,,os.mkdir (dir_image_day_pingjie) import 操作系统      #,os.mkdir (“n1"),, #,创建目录      #,创建文件   #,with 开放(& # 39;1. txt # 39;模式=& # 39;w # 39;), as 文件:   #,,,,,file.write (& # 39; 11111 & # 39;)      #,os.mknod (“n1.txt"),,,, #,创建文件,窗户上面不支持,linux中支持      时间=url  r" D: \ workspace \ test36-demo \ \ Day_4_13 \ n2"研究;   打印(& # 39;n2目录下的内容& # 39;,os.listdir (url),, #列出目录下的所有目录和文件      1. #,os.rename (& # 39; txt # 39;, & # 39; 2. txt # 39;),,, #,文件重命名   print (os.getcwd()),,,, #,获取当前目录(绝对路径,)      #,os.rmdir (& # 39; n1 # 39;),,,, #,删除一个空目录   1. #,os.remove (& # 39; txt # 39;),,, #,删除一个文件         print(“文件/目录是否存在:“,os.path.exists (& # 39; 1. txt # 39;)),,,, #,判断文件/目录是否存在,结果,真/假   打印(& # 39;对象是否为目录:& # 39;,os.path.isdir (& # 39; 2. txt # 39;)),,,,,,,, #,判断目录是否存在,是真/否假   打印(& # 39;对象是否为文件:& # 39;,os.path.isfile (& # 39; 2. txt # 39;)),,,,,,, #,判断文件是否存在,是真/否假      打印(& # 39;文件/目录的绝对路径:& # 39;,os.path.abspath (& # 39; n1/n1.txt& # 39;)),, #,获取文件/目录的绝对路径   打印(& # 39;获取文件的大小:& # 39;,os.path.getsize (& # 39; n1/n1.txt& # 39;)),,,,,,, #,获取文件的大小(单位:b 字节)      时间=url_name  r" D: \ workspace \ test36-demo \ \ Day_4_13 \ n1 \研究n1.txt"   时间=name  os.path.basename (url),,, #,获取文件名/文件夹的名称   dir ,=, os.path.dirname (url),,,, #,获取文件/文件夹的路径   打印(& # 39;dir - - - - -在& # 39;,dir, & # 39; \ t \ \ t # 39;, & # 39;名字——祝辞& # 39;,名称)      打印(& # 39;分离文件名与扩展名:& # 39;,os.path.splitext (& # 39; aa.py& # 39;)),,,,,,,,,,,,, #,只会进行分离,不会判断文件是否真实存在   打印(& # 39;分离路径和文件:& # 39;,os.path.split (& # 39; D: \ w1 \ w2 \ w3 # 39;)),,, #,只会进行分离,不会判断目录及文件是否真实存在               打印(& # 39;\ n \ n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -作业- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 39;)   #,练习一:判断文件夹是否存在,不存在就创建文件夹,存在就进的去,创建一个文件   if  os.path.exists (“n1"),==,错误的:   ,,,os.mkdir (& # 39; n1 # 39;)      os.chdir (& # 39; n1 # 39;),,, #,进入目录   with 开放(& # 39;n1.txt& # 39;模式=& # 39;w # 39;), as 文件:   ,,,file.write(& # 39;我的新的& # 39;)         #,练习二:获取n2文件夹下面的所有内容,删除所有的文件夹   时间=url2  r" D: \ workspace \ test36-demo \ \ Day_4_13 \ n2"研究;   时间=data  os.listdir (url2),,, #列出目录下的所有目录和文件   for 小姐:拷贝数据:   ,,,if  os.path.isdir (os.path.join (url2,我)),==,真的:,,#,判断是否是目录   ,,,,,,,,,os.rmdir (os.path.join (url2,我)),,,,,,,,,,,,,#,是目录则删除         #,练习三:自己实现一个os.path.split 分离目录与文件夹   时间=url_name  r" D: \ workspace \ test36-demo \ \ Day_4_13 \ n1"研究;   时间=name  os.path.basename (url),,, #,获取文件名/文件夹的名称   dir ,=, os.path.dirname (url),,,, #,获取文件/文件夹的路径   null

使用Python怎么处理系统文件目录