Python个人学习笔记

Python语法特点:语法缩进:

E.g.:

if(a == “1”): #缩进为Tab

       Return true

else

       Return false

函数块和主函数:

E.g.:

defPrintFunction (): #函数模块

       print “Hello John!”

 

if__name__ == ‘__main__’: #主函数

       PrintFunction()#调用函数

引入包:

importos #os是重要的类

importre

importshutil

importtime

其他使用到的方法:

s =raw_input("Please Input what words you want to find: ")

# raw_input为交互对象, Please Input what words you want to find为提示语句,输入的值会存入变量s,进行后续操作

TestCasesDir= r’D:\Abc’ #DAbc文件作为路径存入变量

os.path.exists(TestCasesDir) #判断该文件路径是否存在

exit(0)#退出程序

os.walk(TestCasesDir)

#遍历TestCasesDir目录下的所有目录和文件(层套层文件夹)

 

os.listdir(TestCasesDir)#列出TestCasesDir下的目录和文件(单个文件夹)

 

os.path.join(’D:\Abc’,‘1.txt’)

#’D:\Abc’‘1.txt’进行拼接,输出完整路径:D:\Abc\1.txt ,可以多个字符串一同拼接,拼接顺序从左至右

 

files= open (r‘D\Abc\1.txt’,'r')

#打开dabc文件夹下的1.txt并打开读取‘r’为读取模式

 

content= files.read()#files读取的内容全部读取,并存放到变量中

content= files.readlines()#files读取的内容逐行读取,并存放到变量中

 

if content.startswith('#'):#查找行内容是否以#开始的

 

if‘abc’ in content  #判断’abc’有没有出现在读取到的内容中

 

files.close() #关闭文件,很重要!

 

files= open (r‘D\Abc\1.txt’,'w')

#打开dabc文件夹下的1.txt并打开写入内容,‘w’为写入模式

 

files.writelines(content.replace(‘old’, ‘new’))

#打开文件后用writelines方法写入文件,写入的内容为将’new’替换为’old’,replace方法实现

 

 

以下为循环遍历文件小demo:

rootdir= “d:\data”                                  

# 指明被遍历的文件夹

 

forparent,dirnames,filenames in os.walk(rootdir):   

#三个参数:分别返回1.父目录 -- parent 2.所有文件夹名字(不含路径) --dirnames 3.所有文件名字 -- filenames

 for dirname in dirnames:                       #输出文件夹信息

  print "parent is:" + parent

  print "dirname is" + dirname

 

 for filename in filenames:                        #输出文件信息

  print "parent is": + parent

  print "filename is:" + filename

  print "the full name of the file is:" +os.path.join(parent,filename)#输出文件路径信息

         if "txt" in filename:   #判断 txt文件是否在读取的目录文件列表中  

---------------------------------------------------------------------------------------------------------------

eachline= ' RP=Fusion/Test/Dataset/123.f3d' #假定读取到的内容并存入eachline

 

match= re.search(r'^([a-zA-Z]:|//[a-zA-Z0-9_.$ -]+/[a-z0-9_.$ -]+)?((?:/|^)(?:[^//:*?"<>|\r\n]+/)+)',eachline)

 

extname= re.findall(r'\[^.\\/:*?"<>|\r\n]+$', eachline)

 

filename= re.findall(r'[^\\/:*?"<>|\r\n]+$' , eachline)

#用正则表达式去截取eachline中的字段并输入,match输出的是路径,extname输出的是拓展名,filename输出的是文件名

 

filepath= match.group(2)

#将获取到的值只截取Fusion/Test/Dataset并输出

 

filename[0]

#由于filename输出的类型是list,为了输出不带list[''],所以用filename[0]

 

search= 'abc'

start= 0

index= eachline.find(search, start)

#利用find方法,从0开始搜索,目标为’abc’,搜索到abc第一个字符出现的位置并返回。假定输出值为3

 

filepath[index:]

#filepath文件路径搜索到的索引值(例如上个例子的3),最后会将索引3之后的内容全部输出,内容为’Fusion/Test/Dataset/123.f3d’

 

SubFolder= filepath[index:].split('/')[-2]

# 利用split方法,进行分割,将内容通过’/’进行分割,将倒数第二个分割的内容进行输出(-2),输出结果为:Dataset

 

os.makedirs(r'C:\Abc\123')

#makedirs可以创建多层文件夹,这里在C盘目录下Abc文件夹里创建完,再创建123文件夹

 

os.mkdir(r'C:\Abc\123')

#mkdir只能创建单个文件夹,这里只能创建C盘下Abc文件夹

 

importshutil

shutil.copy(SourceFolder,TargetFolder)

#引入shutilshutil.copy能够复制文件从文件夹到另一个文件夹。顺序是shutil.copy (已有文件夹路径,目标文件夹路径)

 

i=123

str(i)

# 123int型,通过str方法将其转换为string

 

Dir=r'E:\TDDownload'

os.system('rmdir'+ Dir +' /s /q') #rmdir可以移除指定文件夹目标内的所有文件

shutil.rmtree(Dir)

#shuti.rmtree同样可以移除指定文件夹目标内的所有文件(推荐此方法)

os.remove(Dir) # remove方法只能删除单个文件

 

sourceCustomed= r‘D:\Abc’

print("{0} is NOT Exist!" .format(sourceCustomed))

#.format函数格式化输出,{0}为输出顺序

 

importtime #引入时间模块

printtime.strftime("Start Copy Time :%Y-%m-%d %X",time.localtime())

#将本机当前的时间通过time.strftime方法输出,格式为年月日%Y-%m-%d%Xtime.localtime()为获取本机时间

 

shutil.move("oldpos","newpos") #移动文件(目录)

 

os.rename("oldname","newname")     #重命名文件(目录),文件或目录都是使用这条命令

shutil.copyfile(src,dst)    #从源src复制到dst中去。当然前提是目标地址是具备可写权限。抛出的异常信息为IOException. 如果当前的dst已存在的话就会被覆盖掉

import platform

platform.system() == 'Windows'

platform.system() == 'Darwin'

#引入platform包,platform.system()能够范围当前系统的名称,Darwin为苹果Mac OS X系统

os.name == 'nt'

os.name == 'posix'

 #同样是返回当前系统的类型和名称,nt为Windows系统,posix为苹果Mac OS X系统

 

import _winreg
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,\r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',)
return _winreg.QueryValueEx(key, "Desktop")[0]  

#该方法是用通过搜寻注册表的方法获取当前Windows系统的桌面路径

os.environ['HOME']

#利用os.environ设置系统环境变量,该结果如果在Mac OS X下执行会返回主页路径

(('/').join(FusionPath.split("/")[:-1])).replace(' ','\ ')

#将List的值通过‘/’连接,并通过‘/’分割输出到倒数第二个字段路径,其中有空格的,用'\ '替换

实例代码:https://github.com/John0731/PythonForPersonal

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lonlon29

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值