一.test model
(一) Set package import path
import os,sys
#1.解决python导包的问题 设置导包的路径
BASE_DIR=os.path.dirname ( os.path.abspath(__file__) )
print(BASE_DIR)
sys.path.remove(BASE_DIR)
print(sys.path)
for i,p in enumerate(sys.path):
print(f"{i+1:>2}",p)
if BASE_DIR not in sys.path:
sys.path.append(BASE_DIR)
#2.系统环境变量
envs=os.environ.get("path")
print("\n",envs)
for i,e in enumerate(envs.split(";")):
print(f"{i + 1:>2}", e)
(二) sys.argv
#coding:utf8 #程序出现编码错误 可能需要该代码
import sys,os
os.system(r"echo %cd%")
for i,a in enumerate(sys.argv):
print(i,a)
print("【程序编码】",sys.stdout.encoding)
os.system(r'ping www.baidu.com -n 2')#直接显示执行结果
"""
执行结果是gbk编码的字符串 pycharm控制台输出是utf8的字符串
【解决办法】修改pycharm编码 一般要修改保持默认utf8
File_setting_Edit_FileEncoding=(全局和方案均为)gbk
"""
C='"F:\Program Files (x86)\jetbrains\python3.8.9\python.exe" '
test_window_cmd=C+ f" { os.path.basename(__file__) } p1 p2 p3"
print(test_window_cmd)
"""复杂的python执行 可设用户或系统环境变量=os.path.dirname(C)"""
(三)optparse
二. init myftp
(一) Create myftp workDirs workFiles
r"""
【初使化 myftp目录文件结构】
0.生成 myftp 需要的目录文件 有些文件暂时没有使用 以后再完善
1.pyProjects(所有方案目录下)/demo1(具体项目)/init.py
2.demo1/myftp(子项目)
"""
import os
def create_dir(dirname):
if os.path.isdir(dirname):
print(f"[警告] 该目录 {dirname} 已存在")
elif os.path.isfile(dirname):
print("[警告]无法创建--因创建目录与文件重名%s"%dirname)
elif not os.path.exists( dirname ):
print("该目录{}创建成功".format(dirname))
os.mkdir(dirname)
else:
print(f"[警告]无法创建该目录{dirname}出现未知异常")
def create_file(file):
if os.path.isdir(file):
print(f"[警告]无法创建 该文件{file}与目录重名")
elif os.path.isfile(file):
print("[警告] 该文件 %s 已存在"%file)
elif not os.path.exists( file ):
print("该文件{}创建成功".format(file))
with open(file, mode="w", encoding="utf8") as f:
pass
else:
print(f"[警告]无法创建该文件{file}出现未知异常")
#有些目录及文件 目前暂时还没有使用 以后再完善
def step1_create_myftp_dirs():
d0, d1, d2 = r"./myftp", r"myftp/server/", r'myftp/client/'
d3 = r'myftp/utils'
dirs = [d0, d1, d2,d3]
for d in ["bin", "conf", "home", r"home/alex","log"]:
dirs.append(d1 + d)
for d in dirs:
create_dir(d)
def step2_create_myftp_server_files():
d0= r"myftp/server/"
s_bin = ["my_server.py"]
s_conf = ["accounts.ini", "settings.py"]
s_core = ["main.py", "management.py"]
s_log= [ d0+r"log/access.log" , d0+r"log/error.log" ]
files = []
for f in s_bin:
files.append(d0 + r"bin/" + f)
[ files.append( d0 + r"conf/" + f ) for f in s_conf ]
files = files + [ d0 + r"core/" + f for f in s_core ]
files = files + s_log
for f in files:
create_file(f)
def step3_create_myftp_client_files():
d0 = r"myftp/client/"
cs = ["myclient.py", ".myclient_db.dat",
".myclient_db.bak", ".myclient_db.dir"]
for f in cs:
create_file(d0+f)
if __name__ == '__main__':
step1_create_myftp_dirs()
step2_create_myftp_server_files()
step3_create_myftp_client_files()
"""
pycharm选中 myftp子项目
右键选择makeDirectory As SorcesRoot
myftp子项目的文件夹 变成 浅蓝色 表明是一个python项目?
myftp/server 需要上述操作吗?
""
(二) server/conf/accouts.py settings.py
import hashlib
def string_write_to_file(data_str, filename):
with open(filename, "w",encoding="utf8") as f:
for line in data_str.strip().split("\n"):
line = line.lstrip()
print(line)
f.write(line + "\n")
print()
def writeTo_server_conf_accounts():
alex = f"""
[alex]
username = alex
name = alex
password = {hashlib.md5("123456".encode("utf8")).hexdigest()}
expire = 2030-12-31
[egon]
username = egon
name = egon
password = 123456
expire = 2029-12-31
"""
string_write_to_file(alex, r"myftp/server/conf/accounts.ini")
def writeTo_server_conf_settings():
configure = """
#coding:utf8
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#HOST = "0.0.0.0" #不是真正意义IP地址=整个网络所有主机 此处=localhost?
HOST = "localhost"
PORT = 9999
USER_BASE_DIR = os.path.join(BASE_DIR, "home")
ACCOUNT_FILE = f"{BASE_DIR}/conf/accounts.ini"
MAX_SOCKET_LISTEN = 5
"""
string_write_to_file(configure,r'myftp/server/conf/settings.py' )
if __name__ == '__main__': # demo1/a1.py
writeTo_server_conf_accounts()
writeTo_server_conf_settings()