title: How could I rename a file in a directory indirectly?
body: <pre><code>import os
  import random
  import time
  
  items = os.listdir(&quot;assets/items&quot;)
  items2 = os.listdir(&quot;assets/items2&quot;)
  
  def for_items():
      countdown = len(items)
      while countdown != 0:
          a = random.choice(items)
          #print(f&quot;The item is {a}&quot;)
  
          b = random.choice(items2)
          #print(f&quot;The item it will get replaced with is {b}&quot;)
          os.rename(a, b)
  
          time.sleep(1)
          countdown = countdown - 1
  
      if countdown == 0:
          time.sleep(3)
          print(f&quot;Countdown = {countdown}&quot;)
  
  for_items()
  </code></pre>
  <p>I have this code right here which basically reads 2 different directories, both of the directories have a certain number of images, it takes the name of one of the images in the second directory and renames one image in the first directory to the same name. But when I run the code I get the following error:</p>
  <pre><code>  File &quot;C:/Users/Pyr0/Desktop/RandomGenerator/RRPG2.py&quot;, line 25, in &lt;module&gt;
      for_items()
    File &quot;C:/Users/Pyr0/Desktop/RandomGenerator/RRPG2.py&quot;, line 16, in for_items
      os.rename(a, b)
  FileNotFoundError: [WinError 2] The system cannot find the file specified: **'image in first directory'** -&gt; **'name of image in second directory'**
  </code></pre>
  <p>I tried adding the following block of code:</p>
  <pre><code>old_file = os.path.join(&quot;directory&quot;, &quot;first image.png&quot;)
  new_file = os.path.join(&quot;directory&quot;, &quot;second image.png&quot;)
  os.rename(old_file, new_file)
  </code></pre>
  <p>and using <code>from Pathlib import Path</code> instead, but no luck.</p>
  <p>I have a feeling that I missed something, or am not aware of something, any help would be appreciated. Thanks in advance!</p>