返回中文,标点符号,数字前后的空格及空格前后的一个字符,方便在长段中快速定位空格位置。需要无视英文单词之间,英文单词与数字之间的空格
import re
text = "这是一个测试,这里有 一些 空格 和 一些符号:,。123。"
regex = r"([\u4e00-\u9fa5\w])(\s+)([\u4e00-\u9fa5\d])"
replaced_text = re.sub(regex, r"\1\3", text)
print(replaced_text)
复制就可以用,有兴趣的可以复制运行一下
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@IDE :PyCharm
@Author :Berserker
@Date :2019-12-18 22:30
@Desc :
'''
import os
import random
import shutil
import time
STORE_PATH = r"D:\UserCopyFileProgram\users" # 读取账户和密码路径
USERS_PATH = os.path.join(STORE_PATH, "users.txt") # 用户名和密码的记录
PUBLIC_QUERY_PATH = r"D:\UserCopyFileProgram\Query" # 启用搜索后会保存到这个路径下的txt文件中
def auth_code():
str_code = "ABCDEFGHIJKLMNOPQUSTRVWSYZ1234567890abcdefghijklmnopqustuvwsyz"
code = ""
for cout in range(4):
code += str_code[random.randint(0, len(str_code) - 1)]
print("验证码:", code)
auth = input("请输入验证码:")
if code.lower() == auth.lower():
return True
else:
again = input("验证码输入错误,是否重新输入(Y/N):")
if again.lower() == "y":
auth_code()
else:
return False
def precise_query():
print(" ************** 欢迎使用精准查找 ************** ")
location = input("请输入需要查询的盘符:")
query_fileName = input("请输入需要查询的文件名(如:1.jpg):")
for root, dirs, files in os.walk(location):
for file in files:
if file == query_fileName:
query_path = os.path.join(root, file)
print("文件的路径为:{}".format(query_path))
if not os.path.exists(PUBLIC_QUERY_PATH):
os.makedirs(PUBLIC_QUERY_PATH)
with open(os.path.join(PUBLIC_QUERY_PATH, "precise_query.txt"), "a") as wstream:
wstream.write("{} : {}\n".format(time.strftime("%Y-%m-%d %H:%M:%S"), query_path))
user_view()
def vague_query():
print(" ************** 欢迎使用模糊查找 ************** ")
vague_location = input("请输入需要查询的盘符:")
vague_filename = input("请输入需要查询的文件概括名:")
for root, dirs, files in os.walk(vague_location):
for file in files:
if vague_filename in file:
vague_path = os.path.join(root, file) # 记录查询到的路径
print("文件绝对路径为:", vague_path)
if not os.path.isdir(PUBLIC_QUERY_PATH):
os.makedirs(PUBLIC_QUERY_PATH)
with open(os.path.join(PUBLIC_QUERY_PATH, "vague_query.txt"), "a") as wstream:
wstream.write("{} : {}\n".format(time.strftime("%Y-%m-%d %H:%M:%S"), vague_path))
user_view()
def copy_file(fn_target_path, fn_str_path):
file_number = 0 # 统计有多少个文件被复制了
list_path = os.listdir(fn_target_path) # 获取当前路径下的所有文件和文件夹名
for result in list_path:
copy_file_name = os.path.join(fn_str_path, result) # 获取复制文件的路径
target_file_name = os.path.join(fn_target_path, result) # 获取目标文件的路径
if os.path.isdir(target_file_name):
if not os.path.isdir(copy_file_name):
os.makedirs(copy_file_name)
copy_file(target_file_name, copy_file_name)
else:
copy_file(target_file_name, copy_file_name)
else:
with open(target_file_name, "rb") as fStream:
fileName = fStream.read() # 读取文件二进制码
with open(copy_file_name, "wb") as wStream:
wStream.write(fileName) # 写入文件二进制码
print("文件复制成功:{}".format(target_file_name))
file_number += 1
print('''
*************************
成功复制了({})个文件
*************************
'''.format(file_number))
def register_user():
user_name = input("请输入用户名:")
pass_word = input("请输入密码:")
affirm_password = input("请确认密码:")
if pass_word == affirm_password:
if auth_code():
if not os.path.isdir(STORE_PATH): # 判断储存目录是否存在
os.makedirs(STORE_PATH) # 创建储存目录
with open(USERS_PATH, "a") as wstream:
wstream.write("{} {}\n".format(user_name, pass_word))
print("恭喜,注册成功!")
return True
else:
print("不好意思,注册失败!")
return True
else:
again = input("两次密码输入不一致是否重新注册(Y/N):")
if again.lower() == "y":
register_user()
else:
return True
def remove_file(remove_filepath):
if not os.path.exists(remove_filepath):
print("需要删除的文件不存在")
user_view()
else:
affirm = input("请输入(Y/N)确认是否删除:")
if affirm.lower() == "y":
if os.path.isdir(remove_filepath):
print("正在删除文件请等待..............")
shutil.rmtree(remove_filepath)
print("文件删除成功!")
else:
print("正在删除文件请等待..............")
os.remove(remove_filepath)
print("文件删除成功!")
else:
user_view()
def login_user():
if not os.path.exists(USERS_PATH):
print("当前没有可用的用户名和密码,请注册后使用!")
return True
login_username = input("请输入你的用户名:")
login_password = input("请输入你的密码;")
with open(USERS_PATH) as rstream:
if auth_code():
while True:
users_code = rstream.readline()
if users_code == "{} {}\n".format(login_username, login_password):
return user_view()
elif users_code == "":
print("登录失败")
return True
else:
again = input("用户名或者密码错误是否重新登陆(Y/N):")
if again.lower() == "y":
login_user()
else:
return True
def user_view():
print('''
**************** 欢迎使用文件管理系统 ****************
1.精 准 搜 索
2.模 糊 搜 索
3.拷 贝 文 件
4.删 除 文 件
5.返 回 上 层
6.退 出
***************************************************
''')
choice = input("请输入需要的选项:")
if choice == '1':
precise_query()
user_view()
elif choice == '2':
vague_query()
user_view()
elif choice == '3':
targetPath = input("请输入需要复制的目录路径:")
if os.path.exists(targetPath):
file_name = targetPath[targetPath.rfind("\\") + 1:] # 获取需要复制的文件目录
copy_file_path = input("请输入需要复制到的路径:")
copy_path = os.path.join(copy_file_path, file_name)
copy_file(targetPath, copy_path)
user_view()
else:
print("系统找不到指定的路径!")
user_view()
elif choice == '4':
remove_filename = input("请输入删除文件的路径:")
remove_file(remove_filename)
user_view()
elif choice == '5':
system_view()
elif choice == '6':
return False
else:
print("不好意思,没有你输入的选项")
def system_view():
print('''
**************** 欢迎使用文件管理系统 ****************
1.登 录 用 户
2.注 册 用 户
3.退 出
***************************************************
''')
choice = input("请输入需要的选项:")
if choice == '1':
return login_user()
elif choice == '2':
return register_user()
elif choice == '3':
return False
else:
print("不好意思,没有你输入的选项")
return True
isFlag = True
while isFlag:
isFlag = system_view()