原始笔记是若干个
## 标题加代码块的并列条目,开头还有一行散落的 nohup 命令。这里按”后台运行 / 读文件 / dict 操作 / 按行执行 shell”四块整理,命令和脚本本身保持原样。
当前保留内容
1. 远程后台执行 Python 脚本并写文件
nohup + tee -a 同时落到日志文件并维持后台运行:
nohup python3 run.py |tee -a 1.log &
2. 读 txt 文件
逐行读 employeeUid.txt、去掉首尾空白后转 int 收集到列表:
f = open("employeeUid.txt")
line = f.readline()
uid_list = []
while line:
line = line.rstrip()
line = line.lstrip()
if line is not None:
uid_list.append(int(line))
#print(int(line))
line = f.readline()
#print(int(line))
f.close()
3. dict 操作
3.1 dict 初始化
用 dict.fromkeys 给一组 key 批量建 dict(默认值 None):
m = range(2048)
mp1 = dict.fromkeys(m)
3.2 打印 dict
for v in mp:
print(str(v) + " " + str(mp[v]))
3.3 dict 排序
按值排序:
print(sorted(key_value.items(), key = lambda kv:(kv[1], kv[0])))
按 key 排序:
# 字典按键排序
for i in sorted (key_value) :
print ((i, key_value[i]), end =" ")
4. 按行执行 shell 命令
把 shell_sql.txt 中每一行作为一条 shell 命令依次执行:
#!/usr/bin/python3
import os
import re
f = open("shell_sql.txt")
line = f.readline()
uid_list = []
while line:
#line = line.rstrip()
#line = line.lstrip()
#if line is not None:
# uid_list.append(int(line))
#print(int(line))
#line = f.readline()
#print(int(line))
os.system(line)
line = f.readline()
f.close()
后续可补的方向
- “读 txt”用
with open(...) as f:上下文管理器重写一份更安全的版本 - “按行执行 shell”用
subprocess.run替代os.system,处理返回码与异常
FEATURED TAGS
Git
Cheat Sheet
Markdown
Tools
C++
Linker
Thread
Linux
TCP
Network
GDB
Debug
leetcode
链表
WSL
Ubuntu
Windows
Linux Kernel
GCC
Android
adb
Troubleshooting
Profiling
Sanitizer
glibc
MySQL
Database
Python
curl
Build
ELF
clang-format
CMake
Graphviz
Performance
vcpkg
Protobuf
排查
速查
内存
STL
调试
性能分析
性能
读书笔记
方法论
架构
网络
Timer
mbedTLS
TLS
安全
负载均衡
脚本
工具
LRU
二叉树
BST
中序遍历
回溯
二分查找
优先队列
排序
旋转数组
jenkins
部署