curl -L -o python-3.8.1.tgz https://www.python.org/ftp/python/3.8.1/python-3.8.1.tgz
tar -zxvf python-3.8.1.tgz
cd python-3.8.1
./configure --prefix=/usr/local/python3
make
make install
ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
sudo rm -R /System/Library/Frameworks/python.framework/Versions/2.7 # mac下卸载系统自带python2.7
pip3 install -r requirements.txt #批量安装txt文件中的package
(path, filename) = os.path.split("D:/test/test.py") #提取目录文件名
(filename, extension) = os.path.splitext("D:/test/test.py") #提取文件名和后缀 (D:/test/test, .py)
file_name = os.path.basename("D:/test/test1.py") #提取文件名 test1.py
text1 = os.path.splitext(file_name)[0] #提取文件名不含后缀 test1
demjson.encode(skillValue) #数组转json字符串
if s is not None:
for idx in range(0, len(array)):
print(idx)
# destPathName = "a/b.txt"
os.makedirs(os.path.dirname(destPathName), exist_ok=True)
shutil.copy(srcPathName, destPathName)
通常__init__.py 文件为空,但是我们还可以为它增加其他的功能。我们在导入一个包时,实际上是导入了它的__init__.py文件 这样我们可以在__init__.py文件中批量导入我们所需要的模块,而不再需要一个一个的导入。
Python的函数体使用缩进来表示,函数体至少应当保持一个空格的缩进。当缩进结束的时候,表示函数体结束。
对于模块和自己写的程序不在同一个目录下,可以把模块的路径通过sys.path.append(路径)添加到程序中。
import sys
sys.path.append('src/mugen/parser')
__import__('pkg_resources').declare_namespace(__name__)
MoreMore2import signal
def signal_handler(signal_number, frame):
print "Proceed ..."
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
import logging
logging.basicConfig(level=logging.DEBUG,#控制台打印的日志级别
filename='new.log',
filemode='a',##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
#a是追加模式,默认如果不写的话,就是追加模式
format=
'%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s' #日志格式
)
logging.info("print to file")
os.path.dirname(os.path.realpath(__file__))
def LoopDirectory(root_dir, out_file_name, includes, excludes, extras = None):
for root, dirs, files in os.walk(root_dir, topdown=True):
dirs[:] = [d for d in dirs if d not in excludes]
for pat in includes:
for f in fnmatch.filter(files, pat):
file_path = os.path.join(root, f)
file = open(file_path, "rb")
s = file.read()
file.close()
def foo(var,x):
return {
'a': lambda x: x+1,
'b': lambda x: x+2,
'c': lambda x: x+3,
}[var](x)
py -3 -m pip install package_name
py -2 test.py # 使用python2.x来执行test.py
py -3.5 test.py # 使用python3.5来执行test.py
代码中使用:
#! python3.7
# -*- coding:utf-8 -*-
import sys
import time
print(sys.version)
time.sleep(3600)
根据第一行的声明来选择使用哪个版本py get_version.py
if sys.version_info[:2] >= (3, 0):
raise RuntimeError('Less than python 3.0 is required')
no module named ‘_ctypes’
MoreSolution:
yum install libffi-devel
然后重新安装python, make && make install
.
max_jobs_running = 8
jobs_running = 0
runningJobs = []
for p in p_l:
p.start()
jobs_running += 1
if jobs_running >= max_jobs_running:
while jobs_running >= max_jobs_running:
jobs_running = 0
for p in p_l:
jobs_running += p.is_alive()
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade --force-reinstall pip
/Users/user/.pyenv/shims/python: line 21: /usr/local/Cellar/pyenv/1.2.20/libexec/pyenv: No such file or directory
Morepyenv rehash
Microsoft Visual C++ 14.0 is required . Get it with “Microsoft Visual C++ Build Tools”
Moreerror: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
Solution:
下载对应whl文件<http://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud>
sys.path.append("./library/ccxt/python")
import ccxt
sshDir = "~/.ssh"
subprocess.run(f"chmod 755 {sshDir}/*", shell=True)
isinstance(jmsgs, dict)
from collections.abc import Mapping
if isinstance(any_object, Mapping):
isinstance(data, (bytes, bytearray))
+ 右对齐;正数前加正好,负数前加负号;
- 左对齐;正数前无符号,负数前加负号;
空格 右对齐;正数前加空格,负数前加负号;
0 右对齐;正数前无符号,负数前加负号;用0填充空白处
print("%(name)+10s————————%(age)-10d————————"%{'name':'xx','age':20})
结果:
xx————————20 ————————
def worker1():
open_socket1()
def worker2():
open_socket2()
threads = []
t = threading.Thread(target=worker1)
threads.append(t)
t.start()
t = threading.Thread(target=worker2)
threads.append(t)
t.start()
os.system('say "hello"')
import re
a = 'abcABC123'
b = re.search('[A-Z](?=[^A-Z]*)',a)
print(b.span(),a[b.span()[0]])
s = 'a good boy, a good girl. a bad man.'
f = re.finditer('good',s)
print(f)
for i in f:
print(i)
print(i.span())
目录结构如下
|-- main.py
|-- test
|-- test1.py
method1:
sys.path.append("test")
import test1
method2:
test目录新建__init__.py
from test import test1
创建目录发生错误: <paramiko.ChannelFile from <paramiko.Channel 2 (EOF received) (open) window=2097152 -> <paramiko.Transport at 0xf2140148 (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>
目录创建失败 看看是否多了一个目录斜杠之类
error in demjson setup command: use_2to3 is invalid