मैंने एक पायथन लिपि लिखी है जो एक फ़ोल्डर संरचना के माध्यम से चक्रित होती है और किसी दिए गए मान के लिए फ़ाइलों के अंदर दिखती है। लिपि अच्छी तरह से काम करती है, लेकिन अब मैं एक जीयूआई जोड़ने की कोशिश कर रहा हूं और इस त्रुटि को पारित नहीं कर सकता। गुई में 'खोज' पर क्लिक करते समय त्रुटि उत्पन्न होती है, जिससे घटना शुरू हो जाती है।
फ़ाइल "C:\Users\xxxxx\Documents\Python Scripts\FileContentsSearcherwWithFileWrite_GUIv02.py", लाइन 66, में घटना, मान = खिड़की ()। पढ़ें विशेषता त्रुटि: 'टपल' ऑब्जेक्ट में कोई विशेषता नहीं है 'पढ़ें'
मैं सोचता हूं मैं समझता हूं कि स्क्रिप्ट में कहीं न कहीं एक टपल है जिसे विंडो पढ़ने/प्रदर्शित करने का प्रयास कर रही है, लेकिन मैं यह नहीं समझ सकता कि यह क्या हो सकता है। क्या कोई मदद कर सकता है?
धन्यवाद
import os, PySimpleGUI as sg
document_ext = ['.SVG', '.txt', '.XML']
layout = [
[
sg.Text("This program can be used to search for a particular \nterm in all files under the folder location provided.")
],
[
sg.Listbox(document_ext, size=(10,5), key="-File_Ext-")
],
[
sg.Text('What would you like to search for?')
],
[
sg.InputText(size=(30,5), key="-Search_Term-")
],
[
sg.Text("Choose Folder to Search:")
],
[
sg.In(size=(30,5), key="-FOLDER-"),
sg.FolderBrowse()
],
[
sg.Text("Where Should Report Be Saved?")
],
[
sg.In(size=(30,5), key="-FOLDER2-"),
sg.FolderBrowse()
],
[
sg.Button(button_text="Search")
],
[
sg.Text(key="-Output-", size=(30,5))
]
]
window = sg.Window("File Contents Searcher", layout)#, margins=(200,200))
def main(svalue, location, ext):
number_found = 0
search_results = ""
os.chdir(location)
for dpath, dname, fname in os.walk(os.getcwd()):
for name in fname:
pat = os.path.join(dpath,name)
if name.endswith(ext):
with open(pat) as f:
if svalue in f.read():
number_found += 1
search_results += "--- \nFilename: {} \nFilepath: {} \n".format(name, pat)
search_results_head = "\"{}\" was found in {} files. \n \n".format(svalue, number_found)
output = "RESULTS \n \n" + search_results_head + search_results
return output, search_results_head
def create_log(sl, s_res):
os.chdir(sl)
print(os.getcwd())
with open("FileSearchResults.txt", "w") as f:
f.write(s_res)
return "Report Saved"
while True:
event, values = window().read
if event == sg.WIN_CLOSED:
break
if event == "Search":
m = main("-Search_Term-", r"-FOLDER-", "-File_Ext-")
c = create_log(r"-FOLDER2-", m[0])
window("-Output-").update(print(m[1] + " " + c))
window.close()
1 उत्तर
आप यह कोशिश कर सकते हैं।
import os, PySimpleGUI as sg
document_ext = ['.SVG', '.txt', '.XML']
layout = [
[
sg.Text("This program can be used to search for a particular \nterm in all files under the folder location provided.")
],
[
sg.Listbox(document_ext, size=(10,5), key="-File_Ext-")
],
[
sg.Text('What would you like to search for?')
],
[
sg.InputText(size=(30,5), key="-Search_Term-")
],
[
sg.Text("Choose Folder to Search:")
],
[
sg.In(size=(30,5), key="-FOLDER-"),
sg.FolderBrowse()
],
[
sg.Text("Where Should Report Be Saved?")
],
[
sg.In(size=(30,5), key="-FOLDER2-"),
sg.FolderBrowse()
],
[
sg.Button(button_text="Search")
],
[
sg.Multiline(key="-Output-", size=(30,5))
]
]
window = sg.Window("File Contents Searcher", layout)#, margins=(200,200))
def main(svalue, location, ext):
number_found = 0
search_results = ""
location = (values["-FOLDER-"]) # Set values to window.read() values
svalue= (values["-Search_Term-"]) # Ditto for this
ext = str(values["-File_Ext-"][0].lower()) # Needs this to choose value and make it case insensitive
#os.chdir(location) # Don't need
for dpath, dname, fname in os.walk(location): #Hardcoded to value above
for name in fname:
pat = os.path.join(dpath,name)
if name.endswith(ext):
with open(pat) as f:
if svalue in f.read():
number_found += 1
search_results += "--- \nFilename: {} \nFilepath: {} \n".format(name, pat)
search_results_head = "\"{}\" was found in {} files. \n \n".format(svalue, number_found)
output = "RESULTS \n \n" + search_results_head + search_results
return output, search_results_head
def create_log(sl, s_res):
s1 = (values["-FOLDER2-"]) # Hardcoded again
print(os.getcwd())
with open("FileSearchResults.txt", "w") as f:
f.write(s_res)
return "Report Saved"
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == "Search":
m = main("-Search_Term-", "-FOLDER-", "-File_Ext-")
print(m)
c = create_log(r"-FOLDER2-", m[0])
window["-Output-"].update(m[1] + " " + c)
window.close()
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।
event, values = window.read()
होना चाहिए।