Home > database >  How to use linux command "cat" in python
How to use linux command "cat" in python

Time:02-06

Input:A.fasta

>A
sldkfjslkdcskd

Input:B.fasta

>B
pofnvkweu

Expected output:A_B.fasta

>A
sldkfjslkdcskd
>B
pofnvkweu

Code:

my_model_dir_path='../my_model'
word='.fasta'

for f in os.listdir(my_model_dir_path):
    if word in f:
        subprocess.call(["cat", f"{f}"])

Error message:

No such file or directory

I used subprocess to call linux command 'cat' to concatenate fasta files in a fasta format like Expected output. However, I got a message like "No such file or directory" even though the files and the directory exist in right path. Could you let me know what's the problem? Thank you.

CodePudding user response:

I am uncertain why you'd want to exec a new command (cat) to do this when you can use Python's built-in file operations to do it instead.

Append a text to file in Python

CodePudding user response:

I saw this post: https://www.delftstack.com/howto/python/python-cat/

import os

os.system("echo 'Hello! Python is the best programming language.' >> ~/file.txt")
os.system("cat ~/file.txt")

Output: Hello! Python is the best programming language.

  •  Tags:  
  • Related