如何在python中创建保护文件的密码?

2024-10-04 01:37:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个程序来创建一个.txt文件,其中包含可执行文件,但我必须输入某种密码来保护它。 我乐于接受新思想。你知道吗

我正在用我想要保存的信息创建一个.txt文件,我试着把这个.txt文件放在一个zip文件中(因为有一种方法可以通过库设置密码),但是我做不到。我愿意接受任何一种想法,可以是一个.txt文件或.xlsx你知道吗

`

import tkinter as tk
import os
from os import path
import shutil
from shutil import make_archive


root = tk.Tk()
root.title("Annulus volume v1.0")

mylabel1 = tk.Label(root, text="Name")
mylabel2 = tk.Label(root, text="Start Pressure in anular")
mylabel3 = tk.Label(root, text="End Pressure in anular")
mylabel4 = tk.Label(root, text="Temp in anular")

mylabel1.grid(row=1, column=1)
mylabel2.grid(row=3, column=1)
mylabel3.grid(row=5, column=1)
mylabel4.grid(row=7, column=1)

mylabel11 = tk.Label(root, text=".")
mylabel21 = tk.Label(root, text="barg")
mylabel31 = tk.Label(root, text="barg")
mylabel41 = tk.Label(root, text="°C")

mylabel11.grid(row=1, column=3)
mylabel21.grid(row=3, column=3)
mylabel31.grid(row=5, column=3)
mylabel41.grid(row=7, column=3)

e1 = tk.Entry(root, width=10)
e1.grid(row=1, column=2)
e2 = tk.Entry(root, width=10)
e2.grid(row=3, column=2)
e3 = tk.Entry(root, width=10)
e3.grid(row=5, column=2)
e4 = tk.Entry(root, width=10)
e4.grid(row=7, column=2)

def volume():

    v = str ((e1.get()))
    pi = float (e2.get())
    pf = float(e3.get())
    t = float (e4.get())


    v1 = float(8.13)
    v2= float (t + 273.15)
    v3 = float (pf-pi)
    v4 =float (v3*100.0)
    anular = float ((v1*v2)/v4)


    if (anular > 45): 
        mylabel5 = tk.Label(root, text="situation: flooded ")
        mylabel5.grid(row=9, column=2)
        mylabel5 = tk.Label(root, text="Valor: " + str(anular)[0:6])
        mylabel5.grid(row=10, column=2)


    else:
        mylabel5 = tk.Label(root, text="situation: dry")
        mylabel5.grid(row=9, column=2)
        mylabel5 = tk.Label(root, text="Valor: " + str(anular)[0:6])

        mylabel5.grid(row=10, column=2) 

    myfile = open(v + ".txt","a")
    myfile.write("Vol: " + str (v) + "\n")
    myfile.write("Start Press: " + str (pi) + "\n")
    myfile.write("End Press: " + str (pf) + "\n")
    myfile.write("Temp: " + str (t) + "\n")
    myfile.close() 


calcbutton = tk.Button(root, text="Calc", command=volume, fg="white", bg="purple")
calcbutton.grid(row=11, column=2)

root.mainloop()

`


Tags: 文件textimporttxtcolumnrootfloatmyfile