'How to run a script for several files [duplicate]

I wrote a simple script that adds a column with a single value to an existing file, I want to run it for several other files located in one folder, any idea how I could do this?

The python script looks something like this:

!/usr/bin/env python

from qi.h5 import *
import numpy as np
import pandas as pd
import sys
import project


path= "/qdata/projects4/solidground/bsh_o13/import/horizon/raw/Unit_X.Q_085"
#output_dir = "/qdata/projects4/solidground/bsh_o13/import/horizon/raw/test"



df = pd.read_csv(files, delim_whitespace = True, header = None)
df[len(df.columns)] = 1
df_no_indices = df.to_string(index=False)


print(df_no_indices)


Solution 1:[1]

If you put code in function which gets path then you can simply run it with different paths

import pandas as pd

# --- functions ---

def my_function(path):
    df = pd.read_csv(files, delim_whitespace=True, header=None)  # PEP8: arguments witout spaces around `=`
    df[len(df.columns)] = 1
    df_no_indices = df.to_string(index=False)
    print(df_no_indices)
    
# --- main ---

first_path = "/qdata/projects4/solidground/bsh_o13/import/horizon/raw/Unit_X.Q_085"
my_function(first_path)

my_function('/other/path/to/file')
my_function('/different/path/to/file')

And if you put paths on list then you can use for-loop

import pandas as pd

# --- functions ---

def my_function(path):
    df = pd.read_csv(files, delim_whitespace=True, header=None)  # PEP8: arguments witout spaces around `=`
    df[len(df.columns)] = 1
    df_no_indices = df.to_string(index=False)
    print(df_no_indices)
    
# --- main ---

my_files = [
    "/qdata/projects4/solidground/bsh_o13/import/horizon/raw/Unit_X.Q_085",
    '/other/path/to/file',
    '/different/path/to/file',
]

for path in my_files:
    my_function(path)

And later you can use os.listdir(), os.walk() or grep.grep() to create list with files - and use this list with for-loop.

Some functions may give filename without directory and they need

path = os.path.join(directory, filename)

to create full path.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1