IndexError: pop from empty list

2024/11/15 0:34:09

I need help. I have no idea why I am getting this error. The error is in fname = 1st.pop()

for i in range(num) :fname = lst.pop()lTransfer   = [(os.path.join(src, fname),           os.path.join(dst, fname)),(os.path.join(src, fname) + ".md5",  os.path.join(dst, fname) + ".md5")]for tFiles in lTransfer:## copy the file#try :shutil.copyfile(tFiles[0], tFiles[1])os.chmod(tFiles[1], 0o777)success += 1except :ErrList.append(sys.exc_info())print(ErrList[-1])x = 0if success != num:msg  = "CopyRandomFilesToFolder src=%s, dst=%s, desired count=%d, Success=%d\n"%(src, dst, num, success)self._oLogger.LocalWriteLog(self._testname, 'CCmgCefHelper', msg, 0)return success

#

Answer

lst has less elements than num

use

for i in range(min(num, len(lst))):`

or something like

for fname in reversed(lst):# reversed to continue the pop order#your code

Explanation

#lets say we have
num = 4
data = [1,2,3]for i in range(num): # range(4) = [0,1,2,3] so it witl repeat you code 4 timesdata.pop() #remove last element
#first 3 times, it works, but at the last one 'data' is empty, so you get an exception

if you do:

for i in range(min(num , len(data))):
# min(num , len(data)) = min(4,3) = 3
# so you get the corrent number of iterations

Finally:

for fname in reversed(data):
#is the same to
for fname in [3,2,1]:
#'reversed' just change the order of your list
#so it will work in this order, 3, 2 and finishes with 1

Hope it helps

https://en.xdnf.cn/q/119702.html

Related Q&A

Cannot import name StandardScalar from sklearn.preprocessing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

unable to solve strptime() issue even after trying all the formats

Im using the following code:data[Input_volTargetStart][1]>time.strptime(data[Dates][1], "%d %b $y")When I try to run it, I get this error:ValueError: time data 04-Jun-99 does not match for…

OSError. locateOnScreen not working in pyautogui

import pyautoguipyautogui.locateOnScreen(photo.png)Error: OSError: Failed to read photo.png because file is missing, has improper permissions, or is an unsupported or invalid format

Insert into table using For In Range and keys of the value

I have a query (sql1) that populates data, and I am trying to insert the outcome of this data (sql1) as well as other inputs into same table.Here is first query (sql1).sql1 = Select Creator_Id, Record…

Django - how to follow some object (not user) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

Python : wildcard query in Elasticsearch

I want to query a field using wildcard in Elasticsearch but the problem is that the search string is stored in a variable and not available statically. The intended query is : body={"query": …

Extracting text between two strings

I am looking to extract the text between two patterns in my text file here is my text:Q2 2016 Apple Inc Earnings Call - FinalONI SACCONAGHI, ANALYST, BERNSTEIN: I have one, and then a follow-up, as wel…

find find with file name as variable in glob

I want to delete specific files from a list of csv files. the file name is something like this: Storm.abc.ibtracs_all.v03r10.csv. where abc is different for each file.I have a list of the abc part (nam…

Remove Special Chars from a TSV file using Regex

I have a File called "X.tsv" i want to remove special characters (including double spaces) (excluding . Single spaces Tabs / -) using regex before i export them to sub files in python I want…

How to inherit a python base class?

dir/||___ __init__.py||___ Base_class.py||___ Subclass.py__init__.py is empty(as mentioned here)/* Base_class.pyclass Employee:numOfEmployees = 0 # Pure class member, no need to overrideraiseAmount = 1…