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=['http://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 = ["<", attribute, ">"]
    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=['http://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=['http://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 <= (len(fs)-1):
        if IsOnline(fs[x]) == "online":
            Laonline = Label(lowerframe, text=fs[x])
            Laonline.grid(column=0, row=x)
        x=x+1

def RunTwo(Function1, Function2):
    Function1()
    Function2()

def DeleteAllFriends():
    fs = "\n\n"
    f = open("Friends.txt", "w")
    f.write(fs)
    f.close()
    FriendCheck()

def DAFPop():
    DAFpopup = Toplevel()
    DAFframe = Frame(DAFpopup)
    DAFframe.grid(columnspan=4, column=0, row=0)
    F1 = DeleteAllFriends
    F2 = DAFpopup.destroy
    Q1 = lambda: RunTwo(F1, F2)
    DAFL1 = Label(DAFframe, text="This delete all of your friends. Are you sure you wish to continue?")
    DAFL1.grid()
    DAFOK = Button(DAFpopup, width=10, text="Yes", command=Q1)
    DAFOK.grid(column=1, row=1)
    DAFNO = Button(DAFpopup, width=10, text="No", command=DAFpopup.destroy)
    DAFNO.grid(column=2, row=1)

frame = Frame(master, bd=5)
frame.grid()

friendlist = Frame(frame, bd=5, width=150, height=400)
friendlist.grid(column=0, row=0, rowspan=15)

lon = Frame(friendlist, bd=2, width=150, height=10)
lon.grid()

Lonline = Label(lon, text="Friends Online")
Lonline.grid(column=0, row=1)
underlined = tkFont.Font(Lonline, Lonline.cget("font"))
underlined.configure(underline=True)
Lonline.configure(font=underlined)

lowerframe = Frame(friendlist, bd=2, width=150, height=390)
lowerframe.grid()

lowerframe.grid_propagate(0)

newfriendframe = Frame(frame, bd=2)
newfriendframe.grid(column=1, row=0)

nfe2 = StringVar()
nfentry = Entry(newfriendframe, width=12, textvariable=nfe2)
nfentry.grid()
nfe2.set("")
nfentry = nfe2.get()

newfriend = Button(newfriendframe, text="Add Friend", width=10, command=AddFriend)
newfriend.grid(column=0, row=1)

oldfriendframe = Frame(frame, bd=2)
oldfriendframe.grid(column=1, row=1)

ofe2 = StringVar()
ofentry = Entry(oldfriendframe, width=12,textvariable=ofe2)
ofentry.grid()
ofe2.set("")
ofentry = ofe2.get()

oldfriend = Button(oldfriendframe, text="Delete Friend", width=10, command=DeleteFriend)
oldfriend.grid(column=0, row=1)

rof = Button(frame, text="Reset List", width=10, command=DAFPop)
rof.grid(column=1, row=2)

update = Button(frame, text="Refresh", width=10, command=FriendCheck)
update.grid(column=1, row=3)

close = Button(frame, text="Exit", width=10, command=master.destroy)
close.grid(column=1, row=4)

master.mainloop()

Và chức năng không hoạt động là IsOnline(), mặc dù tôi đã để lại url in ở đó cho mã tôi đã đăng, chức năng này dường như giữ cho nó chạy mà không có lỗi 90% thời gian, trong khi không có nó thì 100% bị lỗi.

Nó cũng có một tệp văn bản phụ thuộc, Bạn bè. txt

friend1
friend2
friend3

Có vẻ như để tạo QueryESO. xml tốt cho tôi ngay cả khi nó không có ở đó (tất nhiên là khi nó không gặp lỗi). Nếu đây không phải là trường hợp của bạn, một tệp trống có tên là QueryESO. xml sẽ hoạt động tốt vì nó lấy nội dung từ một trang web

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?

1. Chế độ nào sau đây không hợp lệ để mở tệp? . Sử dụng r+, w+ hoặc a+ để thực hiện cả thao tác đọc và ghi bằng một đối tượng tệp.

Chế độ nào là cách hợp lệ để truy cập tệp từ bên trong tập lệnh Python?

Đọc tệp bằng Python . Có nhiều phương pháp có sẵn cho mục đích này. Chúng ta có thể sử dụng phương thức read(size) để đọc số lượng kích thước của dữ liệu. Nếu tham số kích thước không được chỉ định, nó sẽ đọc và trả về đến cuối tệp

Các chế độ mở tệp khác nhau trong Python là gì?

Mở tệp .
Các tệp trong Python có thể được mở bằng hàm open() tích hợp. .
Ở chế độ 'r', tệp sẽ mở ở chế độ đọc. .
Ở chế độ 'w', tệp sẽ mở ở chế độ ghi. .
Ở chế độ 'a', tệp sẽ mở ở chế độ chắp thêm. .
Ở chế độ 'r+', tệp sẽ mở ở chế độ đọc và ghi

Tùy chọn nào sau đây là không chính xác để sử dụng khi mở tệp bằng Python?

Đây cũng là chế độ mặc định khi mở tệp. Do đó, Lựa chọn (b) là một lựa chọn sai. (a) Khi mở tệp để đọc, nếu tệp không tồn tại thì sẽ xảy ra lỗi. (d) Khi bạn mở một tệp để ghi, nếu tệp không tồn tại, một tệp mới sẽ được tạo.