志存高远的zxs 2023-03-02 22:18 采纳率: 26.7%
浏览 22
已结题

python隐藏资源文件

如何隐藏user.txt,不让它出现?(前提是不能破坏程序原本功能)
代码如下:

def wrapper(function):
    def _open(*args, **kw):
        """ 修改路径 """
        args_list = list(args)
        key = '/'.join(__file__.split('\\')[:-1]) + '/'
        args_list[0] = key + args[0]
        if kw.get('file'):
            kw['file'] = key + kw['file']
        return function(*args_list, **kw)
    return _open
open = wrapper(open)
from os import system
from sys import exit
from random import randint
import time
from time import sleep
MAX_USERS=2
user_file='user.txt'
error_time=4
mode=False
user_name=''
user_passwd=[None,None]
try:
   f=open(user_file,'r')
   user_dict=eval(f.read())
   f.close()
except FileNotFoundError:
   f=open(user_file,'w')
   f.write("{'Administrator':'123456'}")
   f.close()
   user_dict={'Administrator':'123456'}
while True:
   user_name=str(input('请输入用户名>'))
   if user_name not in user_dict:
      if user_name=="注销用户":
         while True:
            user_name=input("请输入要注销用户名:")
            user_passwd[0]=input("请输入要注销用户名的密码:")
            if user_name=="Administrator":
               print("这是系统默认用户,不可注销!")
               continue
            if user_name not in user_dict:
               print("用户名或密码错误,请重新输入!")
               continue
            else:
               if user_passwd[0]!=user_dict[user_name]:
                  print("用户名或密码错误,请重新输入!")
               else:
                  dict=user_dict
                  dict.clear()
                  f=open(user_file,'w')
                  f.write("{'Administrator':'123456'}")
                  f.close()
                  user_dict={'Administrator':'123456'}
                  print("注销成功,去注册登录你的新用户吧!")
                  break
      elif len(user_dict)>=MAX_USERS:
          print("用户不存在,且数量已达到最大值,无法继续注册,请输入正确用户名!")
          time.sleep(1)
          continue
      else:
          print("用户不存在,将执行注册操作。")
          if ' ' in user_name:
             print('\aErr: 用户名中不能有空格')
          elif user_name=='':
             print('\aErr: 用户名不能为空')
          else:    
             while True:
                user_passwd[0]=str(input('请设置密码>'))
                if ' ' in str(user_passwd[0]):
                   print('\aErr: 密码中不能含有空格。')
                elif user_passwd[0]=='':
                   print('\aErr: 密码不能为空。')
                elif len(user_passwd[0])<6:
                   print('\aErr: 密码长度太短,至少6位。')
                else:
                   user_passwd[1]=str(input('请再次输入密码>'))
                   if user_passwd[0]!=user_passwd[1]:
                      print('\aErr: 两次输入的密码不一致。')
                   else:
                      print('注册成功!\n\n请重新登录:')
                      user_dict[user_name]=user_passwd[0]            
                      f=open(user_file,'w')
                      f.write(str(user_dict))
                      f.close()
                      break
   else:    
      while error_time!=0:
         user_passwd[0]=input('请输入密码 4次机会/'+str(error_time)+'>')
         if user_passwd[0]!=user_dict[user_name]:
            print('\aErr: 密码错误')
            error_time=error_time-1
         else:
            mode=True
            break
      else:
         print('\n\a\a因错误次数过多,进行验证码确认')
         import random
         mc=''  
         for i in range(0,6):
            ms=random.randint(0,9) 
            mc+=str(ms)
         print("将会在15秒内发送验证码,请耐心等待...")
         eh=random.randint(1,15)
         time.sleep(eh)
         print(mc)
         mi=input("请输入验证码:")
         if mi!=mc:
            print('\aErr: 验证码错误!')
            for i in range(3,-1,-1):
               print('\b'*23+'程序将在 '+str(i+1)+' 秒后退出...',end='',flush=True)
               sleep(1)
            exit(0)
         else:
            error_time=4
   if mode==True:
      break
print('登录成功...')

  • 写回答

1条回答 默认 最新

  • 社区专家-Monster-XH 2023-03-03 00:15
    关注

    基于Monster 组和GPT的调写:

    • 如果你想隐藏user.txt文件,但是不破坏程序原有的功能,可以考虑将user.txt文件放到程序所在目录的上级目录(或其他不容易被找到的目录),并且修改代码中读写文件的路径,使用绝对路径,而非相对路径。这样即使用户知道user.txt的名字,也无法直接访问到它。

    例如,假设你的程序文件在 /home/user/myprogram/ 目录下,你可以将user.txt文件放到 /home/user/ 目录下,然后修改代码中读写文件的路径如下:

    import os
    
    # 获取程序所在目录的绝对路径
    dir_path = os.path.dirname(os.path.abspath(__file__))
    
    # 修改路径
    user_file = os.path.join(dir_path, '../user.txt')
    
    # 读取文件
    with open(user_file, 'r') as f:
        user_dict = eval(f.read())
    
    # 写入文件
    with open(user_file, 'w') as f:
        f.write(str(user_dict))
    
    
    

    这样即使用户知道user.txt的名字,也无法直接访问到它。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月11日
  • 已采纳回答 3月3日
  • 创建了问题 3月2日

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?