0

To concatenate all the TXT files of a folder, it can be done with cat easily:

cat ./tmp*.txt >./tmp/all.txt

However, I want to concatenate all files except one which can be done with the following command as explained similarly here:

cat ./tmp/!(1.txt) >./tmp/all_except_1.txt

These commands work perfectly on the command line, but I am trying to call them from python with os.system command and gives an error

>>> import os
>>> os.system('cat ./tmp/!(1.txt) >./tmp/all_except_1.txt')
sh: 1: Syntax error: "(" unexpected

Does someone know why and how can be solved?

Community
  • 1
  • 1
iblasi
  • 1,269
  • 9
  • 21
  • 4
    `os.system` runs the Bourne or POSIX shell: `sh`. What you are trying is `bash` specific. You will have to prefix your command with `bash -c ` (untested). Where are you doing your `shopt`? – cdarke Aug 19 '15 at 18:54

1 Answers1

3

You need to enable Extended Pattern Matching in bash before call it:

os.system("bash -O extglob -c 'cat ./tmp/!(1.txt) >./tmp/all_except_1.txt'")
svfat
  • 3,273
  • 1
  • 15
  • 34
  • I read in a post that the problem could be related with bash, but I didn't know about that option. Thank you, it works perfectly! – iblasi Aug 20 '15 at 06:53