Chế độ nào không phải là cách hợp lệ để truy cập tệp từ bên trong tập lệnh Python

Đây là nơi nó bắt đầu trở nên kỳ lạ. lạ #1. Tôi có cùng một mã trong các chức năng khác nhau trong tập lệnh của mình, nhưng lỗi chỉ xảy ra đối với một trong số chúng

Kỳ lạ #2. Nếu tôi thay đổi mã này, nó sẽ hoạt động tốt

url=['//API_URL/query/query.aspx?name=', name, '&md=user']
print url
url=''.join[url]
file = open["QueryESO.xml", "w"]
filehandle = urllib.urlopen[url]
for lines in filehandle.readlines[]:
    file.write[lines]
file.close[]

Tôi cũng đã thử di chuyển url in sang sau khi tôi tham gia danh sách, cách này không hoạt động và tôi vừa thử in "", cách này cũng gặp lỗi đã đề cập trước đó

Vì vậy, tôi đoán tôi đã tìm thấy một giải pháp. nhưng bất cứ ai có thể giải thích hành vi này?

CHỈNH SỬA. Đây là toàn bộ mã

import urllib
from Tkinter import *
import tkFont
master = Tk[]

QUERY_ESO = "QueryESO.xml"

def QueryXML[xml, attribute]:
    x = [""]
    x=''.join[x]
    z = xml.split[x, 1]
    x = [""]
    x=''.join[x]
    z=z[1]
    z=z.split[x, 1]
    return z[0]

def AddFriend[]:
    nfentry = nfe2
    name=nfentry.get[]
    url=['//agecommunity.com/query/query.aspx?name=', name, '&md=user']
    url=''.join[url]
    file = open["QueryESO.xml", "w"]
    filehandle = urllib.urlopen[url]
    for lines in filehandle.readlines[]:
        file.write[lines]
    file.close[]
    f = open[QUERY_ESO, "r"]
    xml = f.readlines[]
    f.close[]
    xml=''.join[xml]
    f = open["Friends.txt", "r"]
    filestring = f.read[]
    f.close[]
    fs = filestring.split['\n']
    if name in fs:
        print "Friend Already Added"
    elif xml == "Failed to find user":
        print "User does not exist"
    else:
        fs.append[name]
        fs = '\n'.join[fs]
        f = open["Friends.txt", "w"]
        f.write[fs]
        f.close[]
    nfe2.set[""]
    nfentry = nfe2

def DeleteFriend[]:
    ofentry = ofe2
    name=ofentry.get[]
    f = open["Friends.txt", "r"]
    filestring = f.read[]
    f.close[]
    fs = filestring.split['\n']
    if name in fs:
        fs.remove[name]
        fs = '\n'.join[fs]
        f = open["Friends.txt", "w"]
        f.write[fs]
    ofe2.set[""]
    ofentry = ofe2
    
def IsOnline[name]:
    url=['//API_URL/query/query.aspx?name=', name, '&md=user']
    print url
    url=''.join[url]
    file = open["QueryESO.xml", "w"]
    filehandle = urllib.urlopen[url]
    for lines in filehandle.readlines[]:
        file.write[lines]
    file.close[]
    f = open[QUERY_ESO, "r"]
    xml = f.readlines[]
    f.close[]
    xml=''.join[xml]
    if xml == "Failed to find user":
        print "User does not exist"
    else: 
        datetime = QueryXML[xml, "LastUpdated"]
        datetime = datetime.split['T', 1]
        time = datetime[1].split['Z', 1]
        date = datetime[0]
        print "User", name, "is", QueryXML[xml, "presence"], "as of", date, "at", time[0]
        return QueryXML[xml, "presence"]

def FriendCheck[]:
    f = open["Friends.txt", "r"]
    filestring = f.read[]
    f.close[]
    fs = filestring.split['\n']
    Laonline = Label[lowerframe, text=""]
    Laonline.grid[column=0, row=0]
    Laonline.grid_forget[]
    x=0
    while x 

Chủ Đề