def do_():
import time, sys, pyautogui, os, bs4, requests
a = '登陆成功,'
password = pyautogui.password('请输入密码:')
if password == '134679':
global mode
mode = pyautogui.confirm(a + '请选择模式', buttons=['1.创建文件', '2.删除文件', '3.运行程序', '4.翻译'])
def do():
import time,sys,pyautogui,os,bs4,requests
if open('f.txt','r').read()=='1':
mode = pyautogui.confirm('请选择模式', buttons=['1.创建文件', '2.删除文件', '3.运行程序', '4.翻译'])
if mode == '1.创建文件':
where = pyautogui.prompt('请输入路径(绝对路径或相对路径):')
where = where.replace('\\', '/')
f = open(where, 'w')
x = pyautogui.confirm('是否写入文本?', buttons=['是', '否'])
if x == '是':
f.write(pyautogui.prompt('请输入要写入的文本'))
f.close()
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+')
f.write('1')
f.close()
do()
elif mode == '2.删除文件':
try:
where = pyautogui.prompt('请输入文件路径(绝对路径或相对路径):')
where = where.replace('\\', '/')
os.remove(where)
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
except FileNotFoundError:
pyautogui.alert('找不到文件!')
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
elif mode == '3.运行程序':
try:
name = pyautogui.prompt('请输入程序路径:')
os.system(name)
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
except FileNotFoundError:
pyautogui.alert('找不到程序!')
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
elif mode == '4.翻译':
import requests, sys, bs4, time
words = pyautogui.prompt('请输入要翻译的英文:')
head = {
"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11",
}
try:
url = "http://www.youdao.com/w/" + words + "/#keyfrom=dict2.top"
time.sleep(1)
response = requests.get(url, headers=head)
soup = bs4.BeautifulSoup(response.text, "lxml")
data = soup.find(name="div", class_="trans-container").find_all(name="li")
y = ''
for n in data:
y += n.text
pyautogui.alert('\n单词 '+ words +' 的意思是:'+y)
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
except:
pyautogui.alert("查询出错啦!")
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
else:
pyautogui.alert('密码错误!五秒后退出系统!')
time.sleep(5)
sys.exit()
else:
do_()
if mode == '1.创建文件':
where = pyautogui.prompt('请输入路径(绝对路径或相对路径):')
where = where.replace('\\', '/')
f = open(where, 'w')
x = pyautogui.confirm('是否写入文本?', buttons=['是', '否'])
if x == '是':
f.write(pyautogui.prompt('请输入要写入的文本'))
f.close()
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+')
f.write('1')
f.close()
do()
elif mode == '2.删除文件':
try:
where = pyautogui.prompt('请输入文件路径(绝对路径或相对路径):')
where = where.replace('\\', '/')
os.remove(where)
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
except FileNotFoundError:
pyautogui.alert('找不到文件!')
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
elif mode == '3.运行程序':
try:
name = pyautogui.prompt('请输入程序路径:')
os.system(name)
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
except FileNotFoundError:
pyautogui.alert('找不到程序!')
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
elif mode == '4.翻译':
import requests, sys, bs4, time
words = pyautogui.prompt('请输入要翻译的英文:')
head = {
"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11",
}
try:
url = "http://www.youdao.com/w/" + words + "/#keyfrom=dict2.top"
time.sleep(1)
response = requests.get(url, headers=head)
soup = bs4.BeautifulSoup(response.text, "lxml")
data = soup.find(name="div", class_="trans-container").find_all(name="li")
y=''
for n in data:
y+=n.text
pyautogui.alert('\n单词 '+ words +' 的意思是:'+y)
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
except:
pyautogui.alert("查询出错啦!")
x = pyautogui.confirm('是否继续?', buttons=['是', '否'])
if x == '是':
f = open('f.txt', 'w+').write('1')
f.write('1')
f.close()
do()
do()
with open('f.txt','w') as z:
z.write('')
报错提示:Traceback (most recent call last):
File "E:\python\操作.py", line 188, in
do()
File "E:\python\操作.py", line 104, in do
if mode == '1.创建文件':
UnboundLocalError: local variable 'mode' referenced before assignment