当前位置:天才代写 > tutorial > Python教程 > python 帮同事写的一个切换数据库启动的小工具

python 帮同事写的一个切换数据库启动的小工具

2018-05-18 08:00 星期五 所属: Python教程 浏览:582

python">#!/usr/bin/env python
#coding=utf-8
import os

dir = '.'
dirApp = "%s\Data" % dir
file_type = ".app"
default_file = "default"
use_file = "AppData.mdb"

def re_file(file, tofile):
    os.renames('%s\%s' % (dirApp, file), '%s\%s' %(dirApp, tofile))
    return

def read_config():
    s = '%s\config' % (dirApp)
    opfile = open(s, 'r+')
    content = opfile.readline()
    contents = str.split(content, '=')
    return contents[1]

def write_config(file):
    s = '%s\config' % (dirApp)
    opfile = open(s, 'w')
    opfile.write('local_name=%s' % file)

def add_data():
    new_enter = input("请输入新建名称:")
    try:
        open('%s\%s' % (dirApp, new_enter))
    except FileNotFoundError:
 #       re_file(use_file, '%s%s' % (new_enter, file_type))
        os_str = "copy %s\%s %s\%s%s" % (dirApp, default_file, dirApp, new_enter, file_type)
        os.system(os_str)
    return new_enter

def change_data(enter_name):
    try:
        open('%s\%s' % (dirApp, use_file))
        re_file(use_file, '%s%s' % (read_config(), file_type))
    except FileNotFoundError:
        print(0)
    write_config(enter_name)
    re_file('%s%s' % (enter_name, file_type), use_file)

def start():
    os.system("EPPortal.exe")
    return

data_array = []
ff=tuple(os.walk(dirApp))
array = ff[0][2]
index = 1

local_file = read_config()
if local_file is '':
    print("当前未使用,直接按回车添加")
else:
    print("当前使用: %s(按回车继续使用)" % local_file)

print('')
print("使用其他请输入编号")
print("0 : 新添加")
for file in array:
    filetype = os.path.splitext(file)
    if file_type == filetype[1]:
        data_array.append(filetype[0])
        print('%i : %s' % (index, filetype[0],))
        index = index + 1

enter = input("请输入编号:")

if enter is '':
    if local_file is '':
        enter_name = add_data()
        write_config(enter_name)
        re_file('%s%s' % (enter_name, file_type), use_file)
else:
    ienter = int(enter)
    if ienter is 0:
        enter_name = add_data()
        change_data(enter_name)
    elif ienter > 0 and ienter < index:
        change_data(data_array[ienter - 1])
start()

标签:python

 

    关键字:

天才代写-代写联系方式