python基础-3

 

三元运算  :

1 >>> a =2 
2 >>> b = 5
3 >>> val = a if a < b else b
4 >>> val
5 2
6 >>> val = a if a > b else b
7 >>> val

8 >>> 5

 

文件报错科普:

提示错误OSError: [Errno 22] Invalid argument: ‘文件路径’。

原因是在路径中含有 \ 。在python中 \ 是转义字符、就是这个原因 ,有两种方法,

1、使用 \ \  取消转义 

2、使用反斜杠代表路径、

如下图所示:

 

二进制模式打开文件 

python3 打开文件,会默认以utf-8的格式打开,但是如果你不知道这个文件的格式,可以使用二进制的方式打开文件。

 1 f = open(file='E:\python\\14_test\\lock_file',mode='rb') 

 

   对于不知道什么编码的文件,可以直接使用  chardet 这个第三方工具打开查看。

  result = chardet.detect(open(‘log’,'rb').read))

  print(result)

 打印如下图所示、会打印出字符编码、confidence相似程度为99%。

这里总结一下:

打开文件的模式有三种:
r , 只读模式(默认)。
w,创建模式。【不可读;不存在则创建;存在则删除内容;因为会清空原有文件的内容,一定要慎用】
a, 追加模式。【可读; 不存在则创建;存在则只追加内容;】

   读写混合模式:

    r+:读写模式 ,以读的模式打开,支持写

   w+:写读模式,以写的模式打开,支持读。。。【别用这个了。。。】

注意最后要记得关闭文件:f.close()
w模式(如果没有就创建文件,如果有就清空文件,一定要慎用)
a模式(追加模式,如果没有就创建文件,如果有就把内容追加进去)
r+ 模式(读,追加模式)
w+写读模式, a+追加读模式,
总结

 

其他:fileno  返回文件句柄的索引值--整数

           flush   把文件从内存刷新到硬盘   

           readable   判断文件是否可读 

           tell      返回光标所在字符位置  

           seek   去到字符位置

           readline    光标所在行

           seekable   判断终端位置

           truncate     从当前位置截断文件,保留从头到当前位置  

   

文件修改

 

 1 import os
 2 f_name = 'jianzhi.txt'
 3 f_new_name ='%s.new' %f_name
 4 
 5 old_str = '殇之木乃伊:阿木木(Amumu) 1350'
 6 new_str='-----------------------------------'
 7 
 8 f = open(f_name,mode='r',encoding='utf-8')
 9 f_new = open(file=f_new_name,mode='w', encoding='utf-8')
10 
11 for line in f:
12     if old_str in line:
13         line = line.replace(old_str,new_str)
14 
15     f_new.write(line)
16 
17 
18 f.close()
19 f_new.close()
20 print(f_new_name,f_name)
21 os.rename(f_new_name,f_name)
View Code

这个地方有个问题非常奇怪,如下图所示的截图   ↓

     

 在进行文件重命名时,提示文件已存在,无法创建文件,这个是不应该的,,,暂时还没有想明白。。。

---------------------------------------------------这里记录一下---------------------------------------------------------

windows上用replace别用rename

 

 

 

  函数 :

默认参数  :  默认参数必须放在位置参数后面

 

 

 现写一个简单的员工信息增删改查程序。。。。----------------万恶的分割线-----------------------明天发

 

 

 

 

 

 

                     

            

 

 

 

 

 

 小结:懒惰是万恶之源,发现周末在家的两天里,学习进度不如在公司一边工作一边学习来的效率高,,,以后周末还是可以去外面的798咖啡屋里。。。

 

posted @ 2018-03-18 21:43  Chuan_Chen  阅读(152)  评论(0编辑  收藏  举报
#waifu-toggle { background-color: #fa0; border-radius: 5px; bottom: 66px; color: #fff; cursor: pointer; font-size: 12px; right: 0; margin-right: -100px; padding: 5px 2px 5px 5px; position: fixed; transition: margin-right 1s; width: 60px; writing-mode: vertical-lr; } #waifu-toggle.waifu-toggle-active { margin-right: -40px; } #waifu-toggle.waifu-toggle-active:hover { margin-right: -30px; } #waifu { bottom: -1000px; right: 0; line-height: 0; margin-bottom: -10px; position: fixed; transform: translateY(3px); transition: transform .3s ease-in-out, bottom 3s ease-in-out; z-index: 1; } #waifu:hover { transform: translateY(0); } #waifu-tips { animation: shake 50s ease-in-out 5s infinite; background-color: rgba(236, 217, 188, .5); border: 1px solid rgba(224, 186, 140, .62); border-radius: 12px; box-shadow: 0 3px 15px 2px rgba(191, 158, 118, .2); font-size: 14px; line-height: 24px; margin: -30px 20px; min-height: 70px; opacity: 0; overflow: hidden; padding: 5px 10px; position: absolute; text-overflow: ellipsis; transition: opacity 1s; width: 250px; word-break: break-all; } #waifu-tips.waifu-tips-active { opacity: 1; transition: opacity .2s; } #waifu-tips span { color: #0099cc; } #waifu #live2d { cursor: grab; height: 300px; position: relative; width: 300px; } #waifu #live2d:active { cursor: grabbing; } #waifu-tool { color: #aaa; opacity: 0; position: absolute; left: -10px; top: 70px; transition: opacity 1s; } #waifu:hover #waifu-tool { opacity: 1; } #waifu-tool span { color: #7b8c9d; cursor: pointer; display: block; line-height: 30px; text-align: center; transition: color .3s; } #waifu-tool span:hover { color: #0684bd; /* #34495e */ } @keyframes shake { 2% { transform: translate(.5px, -1.5px) rotate(-.5deg); } 4% { transform: translate(.5px, 1.5px) rotate(1.5deg); } 6% { transform: translate(1.5px, 1.5px) rotate(1.5deg); } 8% { transform: translate(2.5px, 1.5px) rotate(.5deg); } 10% { transform: translate(.5px, 2.5px) rotate(.5deg); } 12% { transform: translate(1.5px, 1.5px) rotate(.5deg); } 14% { transform: translate(.5px, .5px) rotate(.5deg); } 16% { transform: translate(-1.5px, -.5px) rotate(1.5deg); } 18% { transform: translate(.5px, .5px) rotate(1.5deg); } 20% { transform: translate(2.5px, 2.5px) rotate(1.5deg); } 22% { transform: translate(.5px, -1.5px) rotate(1.5deg); } 24% { transform: translate(-1.5px, 1.5px) rotate(-.5deg); } 26% { transform: translate(1.5px, .5px) rotate(1.5deg); } 28% { transform: translate(-.5px, -.5px) rotate(-.5deg); } 30% { transform: translate(1.5px, -.5px) rotate(-.5deg); } 32% { transform: translate(2.5px, -1.5px) rotate(1.5deg); } 34% { transform: translate(2.5px, 2.5px) rotate(-.5deg); } 36% { transform: translate(.5px, -1.5px) rotate(.5deg); } 38% { transform: translate(2.5px, -.5px) rotate(-.5deg); } 40% { transform: translate(-.5px, 2.5px) rotate(.5deg); } 42% { transform: translate(-1.5px, 2.5px) rotate(.5deg); } 44% { transform: translate(-1.5px, 1.5px) rotate(.5deg); } 46% { transform: translate(1.5px, -.5px) rotate(-.5deg); } 48% { transform: translate(2.5px, -.5px) rotate(.5deg); } 50% { transform: translate(-1.5px, 1.5px) rotate(.5deg); } 52% { transform: translate(-.5px, 1.5px) rotate(.5deg); } 54% { transform: translate(-1.5px, 1.5px) rotate(.5deg); } 56% { transform: translate(.5px, 2.5px) rotate(1.5deg); } 58% { transform: translate(2.5px, 2.5px) rotate(.5deg); } 60% { transform: translate(2.5px, -1.5px) rotate(1.5deg); } 62% { transform: translate(-1.5px, .5px) rotate(1.5deg); } 64% { transform: translate(-1.5px, 1.5px) rotate(1.5deg); } 66% { transform: translate(.5px, 2.5px) rotate(1.5deg); } 68% { transform: translate(2.5px, -1.5px) rotate(1.5deg); } 70% { transform: translate(2.5px, 2.5px) rotate(.5deg); } 72% { transform: translate(-.5px, -1.5px) rotate(1.5deg); } 74% { transform: translate(-1.5px, 2.5px) rotate(1.5deg); } 76% { transform: translate(-1.5px, 2.5px) rotate(1.5deg); } 78% { transform: translate(-1.5px, 2.5px) rotate(.5deg); } 80% { transform: translate(-1.5px, .5px) rotate(-.5deg); } 82% { transform: translate(-1.5px, .5px) rotate(-.5deg); } 84% { transform: translate(-.5px, .5px) rotate(1.5deg); } 86% { transform: translate(2.5px, 1.5px) rotate(.5deg); } 88% { transform: translate(-1.5px, .5px) rotate(1.5deg); } 90% { transform: translate(-1.5px, -.5px) rotate(-.5deg); } 92% { transform: translate(-1.5px, -1.5px) rotate(1.5deg); } 94% { transform: translate(.5px, .5px) rotate(-.5deg); } 96% { transform: translate(2.5px, -.5px) rotate(-.5deg); } 98% { transform: translate(-1.5px, -1.5px) rotate(-.5deg); } 0%, 100% { transform: translate(0, 0) rotate(0); } } © 2022 GitHub, Inc. Terms Privacy Securi