Back to Posts

Python学习笔记

Posted in Developer

Python学习笔记

查看当前路径

import os,sys
#获取工作路径(即操作的pwd)
print(os.getcwd())
#获取脚本路径
print(sys.path[0])
# 查看相关数据
print('__file__={0:<35} | __name__={1:<20} | __package__={2:<20}'.format(__file__,__name__,str(__package__)))

路径跳转

#!/usr/bin/python
import os,sys
print __file__
print os.path.realpath(__file__)
print os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
os.chdir("../")
path = "C://"
# Check current working directory.
retval = os.getcwd()
print "Current working directory %s" %retval
# Now change the directory
os.chdir( path )
# Check current working directory.
retval = os.getcwd()
print "Directory changed successfully %s" %retval
#结果:
C:\Users\Administrator\Desktop\55kai>py -2 findme.py
findme.py
C:\Users\Administrator\Desktop\55kai\findme.py
C:\Users\Administrator\Desktop\55kai
Current working directory C:\Users\Administrator\Desktop
Directory changed successfully C:\

搜索文件

import os
import sys
count = 0

def search(dstDir,fileName):
    global count
    #files = [os.path.join(dstDir,x) for x in os.listdir(dstDir)]
    for files in os.listdir(dstDir):
        absPath = os.path.join(dstDir,files)
        if os.path.isdir(absPath):
            try:
                search(absPath,fileName)
            except Exception as e:
                continue
        elif (os.path.isfile(absPath) and os.path.split(absPath)[1].lower()==fileName.lower()):
            count +=1
            print(absPath)
         
search(sys.path[0],"file.py")
print("%d founded"%count)

动态加载模块

#加载模块
__import__(modules_file)
#指定模块
tmp = sys.modules[modules_file]
#模块函数调用
tmp.use(target)

修改文件内容

def alter(file,old_str,new_str):
    """
    替换文件中的字符串
    :param file:文件名
    :param old_str:就字符串
    :param new_str:新字符串
    :return:
    """
    file_data = ""
    with open(file, "r", encoding="utf-8") as f:
        for line in f:
            if old_str in line:
                line = line.replace(old_str,new_str)
            file_data += line
    with open(file,"w",encoding="utf-8") as f:
        f.write(file_data)

alter("file1", "09876", "python")

遍历所有文件

import sys
import os


def get_files(path, rule):
    all_files = []
    for fpath, dirname, fnames in os.walk(path):   # os.walk是获取所有的目录
        for filename in fnames:
            path_name = os.path.join(fpath,filename)
            if path_name.endswith(rule): # 判断是否是"xxx"结尾
                all_files.append(path_name)
    return all_files


def main():
    all_files = get_files(sys.path[0],".py")
    for filename in all_files:
        print(filename)


if __name__ == "__main__":
    main()

with open()

https://www.cnblogs.com/ymjyqsx/p/6554817.html

with open('/path/to/file', 'r') as f:
    print(f.read())
for line in f.readlines():
    print(line.strip()) # 把末尾的'\n'删掉

r 只能读 
r+ 可读可写,不会创建不存在的文件。如果直接写文件,则从顶部开始写,覆盖之前此位置的内容,如果先读后写,则会在文件最后追加内容。
w+ 可读可写 如果文件存在 则覆盖整个文件不存在则创建 
w 只能写 覆盖整个文件 不存在则创建
a 只能写 从文件底部添加内容 不存在则创建
a+ 可读可写 从文件顶部读取内容 从文件底部添加内容 不存在则创建

python2转python3

#2to3.py位于python2.7\Tools\Scripts
#将python2脚本E:\\python2script\\全部转为python3
python2 2to3.py -w E:\\python2script\\

python3与python2兼容性问题

使用popen以系统命令的形式去运行python2脚本

import载入相对路径模块

	start.py
	app/
	        __init__.py
	        sub1/
	                 __init__.py
	                 mod1.py
	         sub2/
	                 __init__.py
	                 mod2.py
1、起点要高,从app同级的start开始。
2mod1.pyfrom ..sub2 import mod2.py

资料:

https://www.napuzba.com/story/import-error-relative-no-parent/

toplevel
├── main.py
└── project
  ├── __init__.py
  ├── config.py
  └── package
      ├── __init__.py
      └── demo.py

    import ..
    import .

print('__file__={0:<35} | __name__={1:<20} | __package__={2:<20}'.format(__file__,__name__,str(__package__)))W

Web API

Flask

http://www.pythondoc.com/flask/quickstart.html#id2

@app.route('/')
def index():
    return 'Index Page'

@app.route('/hello')
def hello():
    return 'Hello World'

@classmethod与@staticmethod

#实现:
#实现仅仅与类交互而不是和实例交互的方法
#并且不要扩散类内部的代码,造成维护困难。
class Kls(object): #类
    no_inst = 0
    def __init__(self):
        Kls.no_inst = Kls.no_inst + 1
    #@classmethod装饰器来创建类方法。不管这个方式是从实例调用还是从类调用,它都用第一个参数把类传递过来。
    @classmethod 
    def get_no_of_instance(cls_obj):
        return cls_obj.no_inst

ik1 = Kls()
ik2 = Kls()
print ik1.get_no_of_instance()
print Kls.get_no_of_instance()
ik1 = Kls()
print ik1.get_no_of_instance()
print Kls.get_no_of_instance()
ik2 = Kls()
print ik1.get_no_of_instance()
print Kls.get_no_of_instance()
#每一次实例化类,都会执行一次。

#@staticmethod只要放不会变化的内容
class Kls2(object):
    def __init__(self, data):
        self.data = data
    def printd(self):
        print(self.data)
    @staticmethod
    def smethod(*arg):
        print('Static:', arg)
    @classmethod
    def cmethod(*arg):
        print('Class:', arg)
        

pip3

python3 -m pip install -r requirement.txt

FIX

1、缺少gcc依赖环境

报错提示:

gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python2.7 -c psutil/_psutil_linux.c -o build/temp.linux-x86_64-2.7/psutil/_psutil_linux.o

unable to execute gcc: No such file or directory

error: command ‘gcc’ failed with exit status

解决: yum -y install gcc

2、缺少python-dev依赖环境

报错提示: psutil/_psutil_linux.c:12:20: fatal error: Python.h: No such file or directory

#include ^ compilation terminated. error: command 'gcc' failed with exit status 1

解决: yum -y install python-devel.x86_64

在centos下,并且是python3的时候,用:

yum install python34-devel

3、win下缺少djcelery

ModuleNotFoundError: No module named 'djcelery'

实际模块名是django-celery

所以要用pip install django-celery

sqlite3

import sqlite3

con = sqlite3.connect("mydb")
con.row_factory = sqlite3.Row

cur = con.cursor()
cur.execute("select name_last, age from people")
for row in cur:
    assert row[0] == row["name_last"]
    assert row["name_last"] == row["nAmE_lAsT"]
    assert row[1] == row["age"]
    assert row[1] == row["AgE"]

遍历字典和数组

#查找Success是否为true
def list_all_dict(dict_a):
    global Success
    if isinstance(dict_a, dict):  # 使用isinstance检测数据类型
        for x in range(len(dict_a)):
            temp_key = list(dict_a.keys())[x]
            temp_value = dict_a[temp_key]
            print("%s : %s" % (temp_key, temp_value))
            list_all_dict(temp_value)  # 自我调用实现无限遍历
    elif isinstance(dict_a, list):
        for x in range(len(dict_a)):
            temp_value = dict_a[x]
            print("%s" % (temp_value))
            list_all_dict(temp_value)  # 自我调用实现无限遍历

pycharm 调试gevent

在Settings->Python Debugger->Gevent compatible打上勾。

(— _— 哭了,一开始不知道,盲调多线程)

django开发

FIX

1、ImportError: cannot import name ‘TEMPLATE_CONTEXT_PROCESSORS’
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "E:\1_Setup\17_python3.6.2\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "E:\1_Setup\17_python3.6.2\lib\site-packages\django\core\management\__init__.py", line 317, in execute
    settings.INSTALLED_APPS
  File "E:\1_Setup\17_python3.6.2\lib\site-packages\django\conf\__init__.py", line 56, in __getattr__
    self._setup(name)
  File "E:\1_Setup\17_python3.6.2\lib\site-packages\django\conf\__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "E:\1_Setup\17_python3.6.2\lib\site-packages\django\conf\__init__.py", line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "E:\1_Setup\17_python3.6.2\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "G:\11_Thinking\3_PocScan\4_MuheScanner\pocscan-master\pocscanui\settings.py", line 72, in <module>
    from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
ImportError: cannot import name 'TEMPLATE_CONTEXT_PROCESSORS'

解决:

Remove this line from your settings:

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

TEMPLATE_CONTEXT_PROCESSORS was deprecated in Django 1.8 and removed in Django 1.10. It’s not possible to import it anymore.

You have already defined context_processors in your TEMPLATES setting, so you don’t need TEMPLATE_CONTEXT_PROCESSORS anymore.

虚拟环境

mkvirtualenv zqxt:创建运行环境zqxt

workon zqxt: 工作在 zqxt 环境 或 从其它环境切换到 zqxt 环境

deactivate: 退出终端环境

其它的:

rmvirtualenv ENV:删除运行环境ENV

mkproject mic:创建mic项目和运行环境mic

mktmpenv:创建临时运行环境

lsvirtualenv: 列出可用的运行环境

lssitepackages: 列出当前环境安装了的包

创建的环境是独立的,互不干扰,无需sudo权限即可使用 pip 来进行包的管理。

subprocess:子程序调用

http://blog.csdn.net/g457499940/article/details/17068277

subprocess.Popen.poll

0 正常结束
1 sleep
2 子进程不存在
-15 kill
None 在运行
self.proc.poll() is not  None判断进行是否结束

多线程

http://chengable.com/index.php/archives/331/

SaltStack 配置gitfs报错yacc table file version is out of date

yum remove libgit2 python-pygit2  
wget https://github.com/libgit2/libgit2/archive/v0.25.1.tar.gz  
tar -zxf v0.25.1.tar.gz && cd libgit2-0.25.1/  
mkdir build && cd build  
cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DLIB_INSTALL_DIR=/lib64  
cmake --build . --target install  
pip install pygit2  
systemctl restart salt-master  

Influence is Power.

Read Next

Linux学习笔记