密碼薄檔案的增刪改查

2021-10-24 12:46:01 字數 3248 閱讀 1975

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# author:torry zhang

# contact: [email protected]

# datetime:2020/10/7 6:17

# software: pycharm

"""作業

2. 製作乙個"密碼薄",其可以儲存乙個**(例如 www.itcast.cn),和乙個密碼(例如 123456),請編寫程式完成這個「密碼薄」的增刪改查功能,並且實現檔案儲存功能

"""import os

filename = "password_note.txt"

# if not os.path.isfile(filename):

if not os.path.exists(filename):

# os.mknod(filename) # windows不支援os.mknod()

os.system("type nul>{}".format(filename))

def read_lines():

"""讀取檔案內容

:return:檔案內容list

"""with open(filename, "r", encoding="utf-8") as f:

return f.readlines() # 返回list

f.close()

def read_all():

"""讀取檔案所有內容

:return:逐行列印內容

"""i = 0

with open(filename, "r", encoding="utf-8") as f:

for line in f.readlines():

i += 1

print(i, "---", line, end="")

f.close()

def insert_content():

"""插入檔案內容

:return:

"""row2 = input("請輸入賬號:")

row3 = input("請輸入密碼:")

with open(filename, "a", encoding="utf-8") as f:

f.write("{}\t\t{}/{}".format(row1, row2, row3) + "\n")

f.close()

def delete_line():

"""刪除檔案內容

:return: 刪除整行內容或列2/列3內容

"""row_num = int(input("將刪除第幾行資料?"))

del_line = read_lines()[row_num - 1]

print("該行資料內容:", del_line)

# todo 重新write該資料,跳過

def alter_line():

"""修改檔案內容

:return: 修改整行內容或列1/2/3內容

"""# todo 修改該行資料

pass

def query_line():

"""檢視檔案內容

:return:全部檢視或檢視某行內容

"""row_nums = len(read_lines())

selection = input("請輸入查詢方式:1.檢視單行資料\t2.檢視所有資料 >>>>>>")

if selection == "1":

row_num = int(input("檢視第幾行資料?"))

if row_num > 0 and row_num <= row_nums:

print("該行資料:", read_lines()[row_nums - 1])

else:

print("目前只有%d行資料,請重新輸入行數。" % row_nums)

elif selection == "2":

print("全量資料內容如下:")

read_all()

print("-" * 50)

else:

print("請輸入正確的行號!")

def menu():

while true:

print("=" * 50)

print("1.增加內容\n2.刪除內容\n3.修改內容\n4.檢視內容\n\n0.退出\n")

option = input("請輸入操作選項:")

# 增加一行內容

if option == "1":

insert_content()

# 刪除某行內容

elif option == "2":

delete_line()

# 修改某行內容

elif option == "3":

alter_line()

# 檢視內容

elif option == "4":

query_line()

elif option == "0":

print("退出當前操作!")

break

else:

print("輸入錯誤,請重新輸入!")

def main():

# 2.操作檔案

menu()

# 3.關閉檔案

if __name__ == "__main__":

main()

遇到問題彙總:

1.windos環境判斷檔案是否不存在,不存在則建立:

# if not os.path.isfile(filename):

if not os.path.exists(filename):

# os.mknod(filename) # windows不支援os.mknod()

os.system("type nul>{}".format(filename))

2.windos建立檔案,可以使用dos命令:type nul> 或 echo nul>

但是 echo 命令會列印回顯內容。

3.執行os命令, os.system() 不需要做回傳值操作,直接執行操作命令。

os.popen() 執行操作命令的同時返回乙個物件。

>>> os.popen("type nul>{}".format(filename))

mysql增刪改查效果 mysql增刪改查

檢視所有資料庫 mysql show databases 建立乙個庫ghd並指定字符集為utp8 mysql create database ghd charset utf8 檢視mysql支援的字符集 mysql show char set 建立乙個表,並設定id為主鍵 create table ...

mysql增刪改查擴充套件 MySQL增刪改查

1 插入 insert 1 insert into 表名 values 值1 值2 例子 insert into t1 values zengsf 23 fengshao 22 2 insert into 表名 欄位1,values 值1 例子 insert into t1 name values ...

python對檔案增刪改查

coding utf8 author bluesli readline 每只讀取檔案的一行,通常也是讀取到的一行內容放到乙個字串變數中,返回str型別,如下圖 2 readlines 每次按行讀取整個檔案內容,將讀取到的內容放到乙個列表中,返回list型別 把檔案內容封裝成字典 defconvert...