I am trying to open excel files from a folder. But I get the error No such file: 'G:\My Drive\Chip data\D060003'

47 Views Asked by At

When I first go into the folder G:\My Drive\Chip data I can retrieve all the names of the excel sheets from that folder. But when I try to use the names of each file using the same directory as before to open each excel sheet I get the error filenotfound. Any help is appreciated!

import os
import glob
import xlwings as xw
import numpy as np
import matplotlib.pyplot as plt
import array
import math

def Excel_Data():

    excels = []
    
    app = xw.apps.active
       
    
    for root, dirs, files in os.walk(r'G:\My Drive\Chip data'):
        for file in files:
            if file.endswith('.xlsx') and not file.startswith('~'):
                file = file[:-5]
                excels.append(file)
       
      
dir = r'G:\My Drive\Chip data'
for i in excels:
    xw.Book(os.path.join(dir, i))
1

There are 1 best solutions below

1
theSPGN On

Try like this:

import os
import xlwings as xw

def Excel_Data():
    excels = []
    app = xw.apps.active
    for root, dirs, files in os.walk(r'C:\Chip data'):
        for file in files:
            if file.endswith('.xlsx') and not file.startswith('~'):
                file = file[:-5]
                excels.append(file)
    return excels

dir = r'C:\Chip data'
excels = Excel_Data()
for i in excels:
    xw.Book(os.path.join(dir, str(i)+".xlsx"))

print(excels) # output: [1, 2]

you are opening file with xw.Book(os.path.join(dir, i)) but it's file without 'xlsx' extension.

If you are opening it from Google drive not a folder from your computer there may be another issue. Use api instread