How can I add for several bed files the header : track type=narrowPeak name=“narrowPeak” preferably in python ,can handle with R
I don't think you need to go to R or Python to do that. In bash
set -euxo pipefail
for f in *.bed
do
  bn=&quot;$(basename ${f})&quot;
  on=&quot;${bn%%.bed}_with_header.bed&quot;
  echo 'track type=narrowPeak name=&quot;narrowPeak&quot;' &gt; &quot;${on}&quot;
  cat &quot;${f}&quot; &gt;&gt; &quot;${on}&quot;
done

EDIT: I just read you want to set the name based on the filename.
EDIT2: And also to add an arbitrary directory
set -euxo pipefail

INDIR=&quot;/path/to/bedfiles&quot;

for f in $(find "${INDIR}&quot; -name &quot;*.bed&quot;)
do
  bn=$(basename "${f}&quot;)
  on=&quot;${bn%%.bed}_with_header.bed"
  dn=$(dirname &quot;${f}")
  echo "track type=narrowPeak name=\"${bn}\&quot;&quot; &gt; &quot;${dn}"/"${on}&quot;
  cat &quot;${f}" &gt;&gt; "${dn}&quot;/&quot;${on}&quot;
done