How do i use Linux terminal commands like CD and LS?
<p>How do i add terminal commands to my Python script? 
I have a huge folder of images/videos/folders with more images/videos, and i want to organize them in a HTML file (FirstPage.html).</p>

<p>The script first lists all the files in the directory:</p>

<pre><code>def listFiles():
  command = "ls"
  output = Popen(command, stdout=PIPE) #This is the only way i found to run LS in terminal and get the output
  x = str(output.stdout.read())
  x = x[2:-3]
  x += (" ")
  x = re.sub(r"\\n", " ", x)
  y = ""
  finalLIST = list()
  for o in x:
    if o == " ":
      finalLIST.append(str(y))
      y = ""
    else:
      y += o
  return finalLIST #returns a list with all files in the current directory
</code></pre>

<p>Then checks if the file is a image or a video, and if its a video, it adds to the HTML file:</p>

<pre><code>&lt;video controls&gt;
  &lt;source src="videoName.mp4" type="video/WebM/mp4"&gt;
  &lt;source src="videoName.ogg" type="video/ogg"&gt;
  Video not suported!
&lt;/video&gt;
</code></pre>

<p>and if its a image it adds:</p>

<pre><code>&lt;img src="ImageName.jpg" alt="image"/&gt;
</code></pre>

<p>The code is:</p>

<pre><code>def organize():
    DIRECTORIES = listFiles()
    IMAGE = [".png", ".jpg"]
    VIDEO = [".webm", ".mp4"]
    for x in DIRECTORIES:
       if not re.search(".", x):
          #This means that it is a directory
          #I want to CD into this directory and run listFiles() and then organize() it. How i do it?
     else:
         for y in IMAGE:
             ADDimg = "\n&lt;img src=\"" + x + "\" alt=\"imagem\"/&gt;\n"
             if re.search(y, x):
                 with open(FirstPage.html) as f:
                     for line in f:
                         if line = "&lt;!--IMAGES--&gt;":
                             f.write(ADDimg)
                         break
                     f.write(ADDimg)
         for y in VIDEO:
             ADDvideo = """\n&lt;video controls&gt;
                   &lt;source src=\"""" + x
             """\" type="video/WebM/mp4"&gt;
                   &lt;source src="video.ogg" type="video/ogg/WebM"&gt;
                   Video not suported!
                &lt;/video&gt;\n
                """
             if re.search(y, x):
                with open(FirstPage.html) as f:
                for line in f:
                     if line = "&lt;!--VIDEOS--&gt;":
                     f.write(ADDvideo)
                     break
</code></pre>

<p>this is FirstPag.html:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;The first page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;!--IMAGES--&gt;


  &lt;!--VIDEOS--&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>I want this script to list the files in the directory, add all images/ videos that are there to the HTML file, then cd into the folders there, and do the same thing, recursively. Any sugestions?</p>

<p>Thanks!</p>