1

I am trying to take in all the files at a given path, and order them based on my data title names. So my data title names are:

data_titles = ['CPU','Physical_Disk','Memory','Network']

The files at this given path are named like 'CPU_data.txt' and 'Memory_data.txt' but there are also some that have more than one file per data title for example 'Physical_Disk_data_1.txt' and 'Physical_Disk_data_2.txt'.

I am trying to create a dicitonary in the style of:

{'Network': 'Network_data.txt', 
 'Physical_Disk': ['Physical_Disk_data_1.txt','Physical_Disk_data_2.txt'], 
 'CPU': 'CPU_data.txt', 
 'Memory': 'Memory_data.txt'}

i.e not overwriting older values

However I keep getting the error AttributeError: 'dict' object has no attribute 'update', if I use append instead of update I get a similar error AttributeError: 'dict' object has no attribute 'append'

table_csv_files={}
    for file_names in os.listdir(Data_folder):
        for name in data_titles:
            if name in file_names:
                if name in table_csv_files:
                    table_csv_files[name].update(file_names)
                    # Have also tried table_csv_files.append({name:file_names})
                else:
                    table_csv_files[name]=file_names

                print table_csv_files

What am I doing wrong?

1
  • Could you set a proper alignment for your code? It does not work. Commented May 26, 2016 at 9:39

4 Answers 4

2

The problem is that your values are sometimes strings (e.g. for Network) and sometimes lists (e.g. for Physical_Disk). If you make sure that they are always lists you can easily append to them:

data_titles = ['CPU','Physical_Disk','Memory','Network']
table_csv_files={}
listdir_output = ['Network_data.txt', 'Physical_Disk_data_1.txt','Physical_Disk_data_2.txt', 'CPU_data.txt', 'Memory_data.txt']
for file_names in listdir_output:
    for name in data_titles:
        if name in file_names:
            if name in table_csv_files:
                table_csv_files[name].append(file_names)
            else:
                table_csv_files[name] = [file_names]
print table_csv_files

Output:

{'Memory': ['Memory_data.txt'], 'Physical_Disk': ['Physical_Disk_data_1.txt', 'Physical_Disk_data_2.txt'], 'Network': ['Network_data.txt'], 'CPU': ['CPU_data.txt']}
Sign up to request clarification or add additional context in comments.

Comments

2

Use a defaultdict whose default value is an empty list, then you can append to the dictionary values without worrying about which keys already exist in your dictionary.

from collections import defaultdict

table_csv_files = defaultdict(list)

for file_names in os.listdir(Data_folder):
     for name in data_titles:
          if name in file_names:
                table_csv_files[name].append(file_names)

print table_csv_files 
# {'CPU': ['CPU_data.txt'], 'Memory': ['Memory_data_1.txt', 'Memory_data_2.txt'], 'Physical_Disk': ['Physical_Disk_data.txt'], 'Network': ['Network_data.txt']}

2 Comments

I didn't know, and according to the documentation is better than using setdefault [python3.5 docs] (docs.python.org/3.5/library/…)
@terencehill Yes. Thanks for stating that.
0

According to Python documentation it's better to use defaultdict(), see the answer by Moses Koledoye

You cannot append to a dictionary. You can append to a value corresponding to a key if that value is a list (or antoher object that implement the append method).

I think this is the best way to make the code easy to follow. The function setdefault add a new list only if the key is not present.

   if name in file_names:
        table_csv_files.setdefault(name, [])
        table_csv_files[name].append(file_names)

Comments

-1

Dictionary[key] = new value

here is a simple example of how you can add/update another value to an existing key in dictionary.

my_dict = {'name': 'Rajiv Sharma', 'city': 'Delhi'}
print my_dict
my_dict['city'] = 'Kuala Lumpur'
print my_dict

output:

{'city': 'Delhi', 'name': 'Rajiv Sharma'}
{'city': 'Kuala Lumpur', 'name': 'Rajiv Sharma'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.