Hướng dẫn which mode is not valid in python? - chế độ nào không hợp lệ trong python?

Tập hợp các câu hỏi và câu trả lời kiểm tra viết Python này tập trung vào các tập tin của Google.

1. Điều nào sau đây không phải là chế độ hợp lệ để mở tệp? A) AB B) RW C) R+ D) W+ Xem Trả lời
a) ab
b) rw
c) r+
d) w+
View Answer

Trả lời: B Giải thích: 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.
Explanation: Use r+, w+ or a+ to perform both read and write operations using a single file object.

2. Sự khác biệt giữa các chế độ R+ và W+ là gì? a) Không có sự khác biệt b) Trong r+ con trỏ ban đầu được đặt ở đầu tệp và con trỏ ở cuối W+ C) trong W+ con trỏ ban đầu được đặt ở đầu tệp và con trỏ ở cuối Đối với R+ D) phụ thuộc vào câu trả lời của hệ điều hành SystemView
a) no difference
b) in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+
c) in w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+
d) depends on the operating system
View Answer

Trả lời: B Giải thích: Không có.
Explanation: none.

3. Làm thế nào để bạn có được tên của một tệp từ một đối tượng tệp (FP)? a) fp.name b) fp.file (tên) c) self .__ name
a) fp.name
b) fp.file(name)
c) self.__name__(fp)
d) fp.__name__()
View Answer

Trả lời: Giải thích: Tên là một thuộc tính của đối tượng tệp.
Explanation: name is an attribute of the file object.

4. Điều nào sau đây không phải là thuộc tính hợp lệ của đối tượng tệp (FP)? a) fp.name b) fp.closed c) fp.mode d)
a) fp.name
b) fp.closed
c) fp.mode
d) fp.size
View Answer

Trả lời: D Giải thích: fp.Size chưa được thực hiện.
Explanation: fp.size has not been implemented.

5. Làm thế nào để bạn đóng một đối tượng tệp (FP)? A) Đóng (fp) b) fclose (fp) c) fp.close () d) fp .__ Đóng __ () Xem câu trả lời
a) close(fp)
b) fclose(fp)
c) fp.close()
d) fp.__close__()
View Answer

Trả lời: C Giải thích: Đóng () là một phương thức của đối tượng tệp.
Explanation: close() is a method of the file object.

6. Làm thế nào để bạn có được vị trí hiện tại trong tệp? a) fp.seek () b) fp.tell () c) fp.loc d) fp.posview Trả lời
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos
View Answer

Trả lời: B Giải thích: Nó đưa ra vị trí hiện tại như một phần bù từ đầu tệp.
Explanation: It gives the current position as an offset from the start of file.

7. Làm thế nào để bạn đổi tên một tập tin? a) fp.name = new_name.txt, b) os.rename (renthing_name, new_name) c) os.rename (fp, new_name) d)
a) fp.name = ‘new_name.txt’
b) os.rename(existing_name, new_name)
c) os.rename(fp, new_name)
d) os.set_name(existing_name, new_name)
View Answer

Trả lời: B Giải thích: OS.RENAME () được sử dụng để đổi tên các tệp.
Explanation: os.rename() is used to rename files.

8. Làm thế nào để bạn xóa một tệp? a) del (fp) b) fp.delete () c) os.remove (‘tệp
a) del(fp)
b) fp.delete()
c) os.remove(‘file’)
d) os.delete(‘file’)
View Answer

Trả lời: C Giải thích: Os.Remove () được sử dụng để xóa các tệp.
Explanation: os.remove() is used to delete files.

9. Làm thế nào để bạn thay đổi vị trí tệp thành giá trị bù ngay từ đầu? a) fp.seek (offset, 0) b) fp.Seek (offset, 1) c)
a) fp.seek(offset, 0)
b) fp.seek(offset, 1)
c) fp.seek(offset, 2)
d) none of the mentioned
View Answer

Trả lời: Giải thích: 0 chỉ ra rằng phần bù liên quan đến bắt đầu.
Explanation: 0 indicates that the offset is with respect to the start.

10. Điều gì xảy ra nếu không có đối số nào được chuyển đến hàm tìm kiếm? a) Vị trí tệp được đặt thành bắt đầu của tệp b) Vị trí tệp được đặt ở cuối tệp C) Vị trí tệp không thay đổi d) Trả lời ErrorView
a) file position is set to the start of file
b) file position is set to the end of file
c) file position remains unchanged
d) error
View Answer

Trả lời: D Giải thích: Seek () mất ít nhất một đối số.
Explanation: seek() takes at least one argument.

Sê -ri Giáo dục & Học tập toàn cầu Sanfoundry - Python.

Để thực hành tất cả các câu hỏi bằng văn bản trên Python, đây là bộ hoàn chỉnh của hơn 1000 câu hỏi và câu trả lời nhiều lựa chọn.

Bước tiếp theo:

  • Nhận Giấy chứng nhận miễn phí trong chương trình Python
  • Tham gia cuộc thi chứng nhận lập trình Python
  • Trở thành một người xếp hạng hàng đầu trong chương trình Python
  • Thực hiện các bài kiểm tra lập trình Python
  • Các bài kiểm tra thực hành theo chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10
  • Các bài kiểm tra giả chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,

Hướng dẫn which mode is not valid in python? - chế độ nào không hợp lệ trong python?

Manish Bhojasia, một cựu chiến binh công nghệ với hơn 20 năm @ Cisco & Wipro, là người sáng lập và CTO tại Sanfoundry. Ông sống ở Bangalore, và tập trung vào sự phát triển của nhân Linux, Công nghệ San, Cvanced C, Cấu trúc dữ liệu & Alogrithms. Giữ kết nối với anh ta tại LinkedIn.Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Đăng ký các lớp học chính miễn phí của mình tại YouTube & Thảo luận kỹ thuật tại Telegram SanfoundryClasses.

Tôi đã gặp phải một lỗi rất kỳ lạ.

Tôi có mã này là một phần của nhiều chức năng của tôi trong tập lệnh Python của tôi:

url=['http://API_URL/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()

Khi tôi chạy nhàn rỗi, mọi thứ đều hoạt động tuyệt vời.

Nếu tôi chạy nó bằng python (dòng lệnh) thì nó sẽ cho tôi lỗi này:

[Errno 22] Chế độ không hợp lệ ('w') hoặc tên tệp: 'queryeso.xml'

Đây là nơi nó bắt đầu trở nên kỳ lạ: Weird #1: Tôi có cùng một mã chính xác 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.

Weird #2: Nếu tôi thay đổi mã thành này, nó 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 không hoạt động và tôi chỉ thử in "", cũng có lỗi được đề 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 có ai có thể giải thích hành vi này không? Tôi có làm điều gì sai? (Và bạn có cần tôi đăng toàn bộ tập lệnh để bạn có thể gây rối với nó không?)

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 đã để url in trong đó cho mã tôi đã đăng, dường như giữ nó chạy mà không có lỗi 90% thời gian, trong khi không có lỗi, nó bị lỗi 100% của thời gian.

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

friend1
friend2
friend3

Nó dường như tạo ra queryeso.xml tốt cho tôi ngay cả khi nó không có ở đó (tất nhiên 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 Queryeso.xml sẽ hoạt động tốt, vì nó nhận được nội dung của nó từ một trang web.

Nút 'Làm mới' là mã có lỗi trong đó.

Chế độ nào không phải là chế độ hợp lệ để truy cập một tệp trong Python?

Điều nào sau đây không phải là chế độ hợp lệ để mở tệp?Giải thích: 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.

Điều nào sau đây không phải là chế độ mở tệp hợp lệ?

Đây là chuyên gia đã xác minh câu trả lời đọc (), write () và append () sẽ mở tệp được chỉ định để truy cập (đọc, viết hoặc nối tiếp) nhưng phương thức đóng () sẽ đóng tệp mở có nghĩa là chúng tôi không thể đọc hoặc viết nữa.Do đó, đóng () không phải là chế độ truy cập tệp thích hợp.close() is not the proper file accessing mode.

Chế độ R+ trong Python là gì?

R có nghĩa là đọc tệp;R+ có nghĩa là đọc và viết tập tin.W có nghĩa là viết tệp;W+ có nghĩa là đọc và viết tập tin.A có nghĩa là viết tập tin, chế độ nối;A+ có nghĩa là đọc và ghi tệp, chế độ nối.reading and writing the file. The w means writing file; w+ means reading and writing the file. The a means writing file, append mode; a+ means reading and writing file, append mode.

AB có phải là một chế độ hợp lệ để mở một tệp không?

AB.Mở một tập tin để nối thêm ở định dạng nhị phân.Con trỏ tệp ở cuối tệp nếu tệp tồn tại.Đó là, tệp ở chế độ phụ lục.. Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode.