2

I am trying to combine a directory of CSV files within a Python script. I have tried using Pandas to achieve this but it seems inefficient as a OS command could achieve the same thing without a library like this...

copy *.csv merged_files.csv

Is there a way to run an OS command like this inside of a Python3 script?

pppery
  • 3,731
  • 22
  • 33
  • 46
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • Look at [subprocess](https://docs.python.org/3/library/subprocess.html). E.g., `subprocess.run(['copy', '*.txt', 'merged.txt'], shell=True)` –  May 13 '20 at 13:41
  • Does this answer your question? [Import multiple csv files into pandas and concatenate into one DataFrame](https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe) – David May 13 '20 at 14:14

1 Answers1

2

If you don't need to retrive the output of the command to put it in a variable you can simply use:

import os
os.system('your command here') 

This will make your script execute a shell command when run.

EDIT: I stumbled upon a much more well written and advanced answer from someone for sure more experienced than me on the topic, please refer to it for a more in-depth view on your question!

Kastakin
  • 121
  • 1
  • 9