4

I've tried to write a script that moves files from one folder to a selection of folders depending on what I have named the file.

For example, 'Physics - a' would be moved from the 'ts' folder to '/Physics/Assignments' in an effort to organize my notes.

This runs continuously in the background and allocates these files new homes the moment that something is placed into the 'ts' folder.

My script works, but after allocating two files, I get the following error:

line 14, add = name.split('-') ValueError: not enough values to unpack (expected 2, got 1).

I don't understand why this occurs, nor how to fix it.

import os
import time
from datetime import date

def clean():

    os.chdir('/Users/Chadd/Desktop/ts')

    i = 0
    while i < len(os.listdir()):
        i = 0
        
        name, ext = os.path.splitext(os.listdir()[i])
        code, add = name.split('-')
        folder = code.strip().upper()

        if add.strip() == 'a':
            add = 'Assignments'

        if add.strip() == 'p':
            add = 'Past Papers'

        if add.strip() == 'n':
            add = 'Notes'

        if add.strip() == 't':
            add = 'Tutorials'

        today = date.today()

        os.rename(
        '/Users/Chadd/Desktop/ts/{}'.format(os.listdir()[i]),
        '/Users/Chadd/Desktop/{}/{}/{}'.format(folder, add, folder + ' - ' + add[:-1] + ' (' + str(today) + ')' + ' - ' + str(1 + len(os.listdir('/Users/Chadd/Desktop/{}/{}'.format(folder, add.strip())))) + ext)
         )

        if len(os.listdir()) == 0:
            break

while True:
    clean()
    time.sleep(1)
ChaddRobertson
  • 605
  • 3
  • 11
  • 30

4 Answers4

5

When you assign two variables like this:

code, add = name.split('-')

Python expects the right-hand part to contain two values. That's why it said:

not enough values to unpack (expected 2)

It expected two values. You probably tried to split a filename that had no - symbol, because Python received only one value after the split. That's why it said:

(expected 2, got 1)

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
3

Problem: Some of your values in name do not have - in them. Hence, name.split('-') would return a list with only one item in it. As you have assigned that list to two variables, your code would look like this:

name,ext = ['some name']

As you can see, python doesn't get enough values to assign, it throws ValueError: not enough values to unpack (expected 2, got 1)

Solution: Check name contains only one -. If it contains more than one -, it will throw ValueError: too many values to unpack (expected 2)

Suman Niroula
  • 343
  • 3
  • 11
2

name do not contains a dash(-). You are trying to split by -, and left side assign to separate variable, and right side to another variable. You should check if name contains -, before splitting.

404pio
  • 1,080
  • 1
  • 12
  • 32
2

Line 14,

code, add = name.split('-')

expects a tuple with two elements, the first of which it assigns to code and the second to add. When you run into the error, name must be set to a value which does not have a '-' in it, so the split command returns a single value.

mermaldad
  • 322
  • 2
  • 7
  • 404pio and Nicolas Gervais beat me to the answer, so while I'm leaving this up in case it is a helpful alternate answer, you should award the correct answer to one of them. – mermaldad Dec 02 '19 at 14:04