Code bị lỗi "ValueError: need more than 1 value to unpack"

các pro giúp em đây là lỗi gì ạ?

Traceback (most recent call last):
  File "C:\Users\HP\Desktop\nana.py", line 3, in <module>
    script, filename = argv
ValueError: need more than 1 value to unpack

cái này là code:

from sys import argv

script, filename = argv

txt = open(filename)

print"her's your file %r:" % filename
print txt.read()

print" Type the filename agin:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

Thiếu tham số ở command line truyền vào argv.Vd bạn chạy ct: >python script.py filename

1 Like

em chưa hiểu lắm anh có thể nó rõ hơn

Nghĩa là bạn phải ghi rõ tên file bạn muốn mở để chương trình thực hiện được đoạn lệnh này

1 Like

em ghi rõ rồi mà nó có chạy được đâu

Để lệnh gán script, filename = argv chạy dc thì argv phải có 2 phần tử trở lên. Mà argv lấy ở đâu? cái này chỉ có ở chế độ command line bạn mới đưa vào dc.

Bình thường bạn chạy 1 chương trình python thì đơn giản gõ:

>python a.py

ở đây argv=["a.py"]

nếu bạn thêm filename để chạy

>python a.py "some file name here"

ở đây argv=["a.py",","some file name here"]
thì lúc đó filename mới có giá trị và chương trình mới chạy dc

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?