1

I have a folder at the moment containing various files that I need to run some code on and convert into a csv. I'm trying to see if there is a way to check the name of the file in the folder and then execute the respective code. I have tried:

for xlsb_file in pathlib.Path('Directory Path').glob('Filename*.xlsb'):
RUN CODE
convert dataset into a csv

For some reason this code seems to execute without any error but no csv file is outputted. Ideally what I would like is when I run the code, for the code to detect the filename, run the respective code and then spit out a csv.

Is there anyway to do this? All help is appreciated!

2
  • Could a solution be to supply the directory name via command line argument? Commented Mar 31, 2019 at 23:29
  • Of course you can analyze the file names and take appropriate action. What’s your question about doing so? Commented Apr 1, 2019 at 2:15

1 Answer 1

1

You can try:

import os

def convert_to_csv(filename):
    """this function converts and save a csv version of the file"""

def convert_this_file(filename):
    """this functions checks if a file should be converted"""
    return filename.endswith('.xlsb')

for root, folders, files in os.walk('Directory Path'):
    for file in files:
        filename = os.path.join(root, file)
        if convert_this_file(filename):
            convert_to_csv(filename)
    break

Remove the 'break' if you want the code scan sub directories recursively

Sign up to request clarification or add additional context in comments.

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.