i am getting a ValueError message when running my code i am currently converting from 2 to 3 and have come across a brick wall any help would be great :)

# sorts files by modifed date, collects the 6 most recent files, and delete all others. #created by dan holding v0.1 (23 Aug 2010) import os, glob, time, marshal, shutil os.mkdir("file_back_ups") root = '.' date_file_list = () date_file_set = () #\/ puts files in root folder in to list for folder in glob.glob(root): print ("folder =", folder) for file in glob.glob(folder + '\*.txt*'): stats = os.stat(file) lastmod_seconds = stats[8] date_file_tuple = lastmod_seconds, file date_file_list= tuple(date_file_tuple) date_file_set = set(date_file_list) date_file_list = list(date_file_set) #date_file_list.sort(reverse = True) #print len "date_file_list" date, count = '', 0 #\/works tho files and finds the 6 latest files for lastmod_seconds, val in date_file_list: folder, file_name = os.path.split(val) lastmod_tuple = time.localtime(lastmod_seconds) file_date = time.strftime("%d/%m/%y", lastmod_tuple) if file_date != date: date=file_date count += 1 else: continue file_time = time.strftime("%H:%M:%S", lastmod_tuple) #print ("%-40s %10s %10s") % (file_name, file_date, file_time) if count >6: break #\/ moves the 6 files to a temp folder dst_file = ".\\file_back_ups\\" shutil.move(file_name, dst_file) #\/ removes all other files for f in glob.glob ('.\*.txt*'): os.unlink (f) dst_fo2 = ".\\" #\/ moves the 6 files back to the root folder for file in glob.glob("dst_file" + '\*.txt*'): shutil.move (file, dst_fo2) os.rmdir("file_back_ups")

[/code=python] and the error message i receive is [code=python] Traceback (most recent call last): File "C:\Documents and Settings\Administrator\My Documents\textprocessing\test files\keep6file.py", line 22, in <module> for lastmod_seconds, val in date_file_list: [/code=python] ValueError: too many values to unpack

i can see that it is a problem with unpacking the values but it worked fine in py2 so what have they changed or modified in py3 to make this stop working[code=python]
# sorts files by modifed date, collects the 6 most recent files, and delete all others.
#created by dan holding v0.1 (23 Aug 2010)
import os, glob, time, marshal, shutil
os.mkdir("file_back_ups")
root = '.'
date_file_list = ()
date_file_set = ()
#\/ puts files in root folder in to list
for folder in glob.glob(root):
print ("folder =", folder)
for file in glob.glob(folder + '\*.txt*'):
stats = os.stat(file)
lastmod_seconds = stats[8]
date_file_tuple = lastmod_seconds, file
date_file_list= tuple(date_file_tuple)
date_file_set = set(date_file_list)
date_file_list = list(date_file_set)
#date_file_list.sort(reverse = True)
#print len "date_file_list"
date, count = '', 0
#\/works tho files and finds the 6 latest files
for lastmod_seconds, val in date_file_list:
folder, file_name = os.path.split(val)
lastmod_tuple = time.localtime(lastmod_seconds)
file_date = time.strftime("%d/%m/%y", lastmod_tuple)
if file_date != date:
date=file_date
count += 1
else:
continue
file_time = time.strftime("%H:%M:%S", lastmod_tuple)
#print ("%-40s %10s %10s") % (file_name, file_date, file_time)
if count >6:
break
#\/ moves the 6 files to a temp folder
dst_file = ".\\file_back_ups\\"
shutil.move(file_name, dst_file)
#\/ removes all other files
for f in glob.glob ('.\*.txt*'):
os.unlink (f)
dst_fo2 = ".\\"
#\/ moves the 6 files back to the root folder
for file in glob.glob("dst_file" + '\*.txt*'):
shutil.move (file, dst_fo2)
os.rmdir("file_back_ups")

[/code=python]
and the error message i receive is

Traceback (most recent call last): File "C:\Documents and Settings\Administrator\My Documents\textprocessing\test files\keep6file.py", line 22, in <module> for lastmod_seconds, val in date_file_list: [/code=python] ValueError: too many values to unpack

i can see that it is a problem with unpacking the values but it worked fine in py2 so what have they changed or modified in py3 to make this stop working[code=python]
Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\My Documents\textprocessing\test files\keep6file.py", line 22, in <module>
for lastmod_seconds, val in date_file_list:
[/code=python]
ValueError: too many values to unpack

i can see that it is a problem with unpacking the values but it worked fine in py2 so what have they changed or modified in py3 to make this stop working

Recommended Answers

All 2 Replies

for sum reason it didnt like that code here it is again with tabs and python coding sorting but still same problem

# sorts files by modifed date, collects the 6 most recent files, and delete all others.
#created by dan holding v0.1 (23 Aug 2010)
import os, glob, time, marshal, shutil
os.mkdir("file_back_ups")
root = '.'
date_file_list = ()
date_file_set = ()
#\/ puts files in root folder in to list
for folder in glob.glob(root):
    print ("folder =", folder)
    for file in glob.glob(folder + '\*.txt*'):
        stats = os.stat(file)
        lastmod_seconds = stats[8]
        date_file_tuple = lastmod_seconds, file
        date_file_list= tuple(date_file_tuple)
        date_file_set = set(date_file_list)
        date_file_list = list(date_file_set)
        #date_file_list.sort(reverse = True)
#print len "date_file_list"
date, count = '', 0
#\/works tho files and finds the 6 latest files
for lastmod_seconds, val in date_file_list:
    folder, file_name = os.path.split(val)
    lastmod_tuple = time.localtime(lastmod_seconds)
    file_date = time.strftime("%d/%m/%y", lastmod_tuple)
    if file_date != date:
        date=file_date
        count += 1
    else:
        continue
    file_time = time.strftime("%H:%M:%S", lastmod_tuple)
    #print ("%-40s %10s %10s") % (file_name, file_date, file_time)
    if count >6:
        break
#\/ moves the 6 files to a temp folder
    dst_file = ".\\file_back_ups\\"
    shutil.move(file_name, dst_file)
#\/ removes all other  files
for f in glob.glob ('.\*.txt*'):
       os.unlink (f)
dst_fo2 = ".\\"
#\/ moves the 6 files back to the root folder
for file in glob.glob("dst_file" + '\*.txt*'):
    shutil.move (file, dst_fo2)
os.rmdir("file_back_ups")

for lastmod_seconds, val in date_file_list:

Obviously you should print at least part of date_file_list. No one here has ESP AFAIK, so no one can discern the contents of date_file_list, but what happens if no file is found.

date_file_tuple = lastmod_seconds, file
        date_file_list= tuple(date_file_tuple)
        date_file_set = set(date_file_list)
        date_file_list = list(date_file_set)

"""  You can replace the above lines of code with
"""
date_file_list = [lastmod_seconds, file]   ## creates a list

"""  note also that you do not append or add items, so the tuple, set, list will
     only contain one item
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.