Error when using Python to output network and switch data to csv

Solved
AnthonyCho
Here to help

Error when using Python to output network and switch data to csv

Hello Forum

 

I have written the following code to output Networks and switches for my organisation

 

import csv
import meraki
import requests

# Replace with your Meraki API Key
API_KEY = 'MYORGAPIKEY'

# Replace with your organization ID
ORG_ID = 'MYORGID'

# Initialize the Meraki dashboard API client
dashboard = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True)

# Fetch a list of all networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(ORG_ID)

# Prepare CSV output
with open('merakiNetworksAndSwitches.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['NetworkID','NetworkName', 'DeviceName', 'Serial', 'Model', 'MAC','IP'])

# Iterate through networks
for network in networks:
NetworkID = network['id']
NetworkName = network['name']

# Get devices in each network
devices = dashboard.networks.getNetworkDevices(NetworkID)

for device in devices:
DeviceName = device.get('name', 'N/A')
Serial = device.get('serial')
Model = device.get('model', 'N/A')
MAC = device.get('mac', 'N/A')
IP = device.get("lanIp", 'N/A')
writer.writerow({
NetworkID,
NetworkName,
DeviceName,
Serial,
Model,
MAC,
IP
})

print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")

 

However when I run this it comes up with the error

 

Traceback (most recent call last):
File "<stdin>", line 14, in <module>
ValueError: I/O operation on closed file.
>>> print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")

 

Can someone please help?

1 Accepted Solution
jimmyt234
Building a reputation

Perhaps you have an indentation error as your code works for me, only thing I changed was the curly braces {} to square ones [] when you are doing the writer.writerow within the devices for loop.

 

 

import csv
import meraki
import requests

# Replace with your Meraki API Key
API_KEY = 'dontlookatmykey'

# Replace with your organization ID
ORG_ID = 'xxxx'

# Initialize the Meraki dashboard API client
dashboard = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True)

# Fetch a list of all networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(ORG_ID)

# Prepare CSV output
with open('merakiNetworksAndSwitches.csv', mode='w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['NetworkID','NetworkName', 'DeviceName', 'Serial', 'Model', 'MAC','IP'])

    # Iterate through networks
    for network in networks:
        NetworkID = network['id']
        NetworkName = network['name']
        
        # Get devices in each network
        devices = dashboard.networks.getNetworkDevices(NetworkID)

        for device in devices:
            DeviceName = device.get('name', 'N/A')
            Serial = device.get('serial')
            Model = device.get('model', 'N/A')
            MAC = device.get('mac', 'N/A')
            IP = device.get("lanIp", 'N/A')
            writer.writerow([
            NetworkID,
            NetworkName,
            DeviceName,
            Serial,
            Model,
            MAC,
            IP
            ])

print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")

 

View solution in original post

5 Replies 5
jimmyt234
Building a reputation

Perhaps you have an indentation error as your code works for me, only thing I changed was the curly braces {} to square ones [] when you are doing the writer.writerow within the devices for loop.

 

 

import csv
import meraki
import requests

# Replace with your Meraki API Key
API_KEY = 'dontlookatmykey'

# Replace with your organization ID
ORG_ID = 'xxxx'

# Initialize the Meraki dashboard API client
dashboard = meraki.DashboardAPI(API_KEY, output_log=False, suppress_logging=True)

# Fetch a list of all networks in the organization
networks = dashboard.organizations.getOrganizationNetworks(ORG_ID)

# Prepare CSV output
with open('merakiNetworksAndSwitches.csv', mode='w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['NetworkID','NetworkName', 'DeviceName', 'Serial', 'Model', 'MAC','IP'])

    # Iterate through networks
    for network in networks:
        NetworkID = network['id']
        NetworkName = network['name']
        
        # Get devices in each network
        devices = dashboard.networks.getNetworkDevices(NetworkID)

        for device in devices:
            DeviceName = device.get('name', 'N/A')
            Serial = device.get('serial')
            Model = device.get('model', 'N/A')
            MAC = device.get('mac', 'N/A')
            IP = device.get("lanIp", 'N/A')
            writer.writerow([
            NetworkID,
            NetworkName,
            DeviceName,
            Serial,
            Model,
            MAC,
            IP
            ])

print(f"Network And Switches Data Saved To merakiNetworksAndSwitches.csv")

 

AnthonyCho
Here to help

Thanks Jimmy for the feedback, my indention has been corrected slightly (indents after the writer.writerow were one too many in) however it's still not working...

AnthonyCho
Here to help

Thanks Jimmy; it was the indentation after all...

mlefebvre
Building a reputation

"with" is a special statement in Python that ensures that the file reference is closed appropriately once you finish this code block, so everything you are doing with the file must happen in an indentation level that is below the "with", the moment your indentation level goes back to the same place as "with" it will close the file.

with blank as blank:
    do something with file
    keep doing something with file
    etc etc

file reference will close automatically when it hits this line

AnthonyCho
Here to help

Thanks for the tip...

Get notified when there are additional replies to this discussion.
Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.