python-3.xbashsubprocesssys

How to change part of files names using Sys and Subprocess modules and Linux Bash


I have Python script which reads files names from oldFiles.txt, iterate through files names which are files in a Linux directory, then update the files names using string.replace and finally run a subprocess to find those files (let's say in home directory to make the explanation and demo easier) and change their names using

subprocess.run(['mv', 'oldfile', 'newfile')]

Let's suppose I have files names with string "jane" and I want to replace it with "jdoe", a file's name example would be: abcd_jane.csv modified into abcd_jdoe.csv

The sys.argv[1] is the argument which will be passed like the follow

./changeJane.py oldFiles.txt

I am using those empty files for local training, their names are saved in oldFiles.txt

sdafsdfs_jane.doc 12fgdf-jane.csv files_jane.txt

Iterating and updating files names in oldFiles.txt is ok or maybe there is some mistakes also :-( but I still couldn't figure out how to run the subprocess to achieve my goal like described above. This is what I came in, thank you very much.

PS: I am forced to use sys and subprocess modules, and also forced to apply all the mentioned details, its a homework assessment.

#!/usr/bin/env python3
import sys
import subprocess
with open(sys.argv[1], 'r+') as f:
  for line in f.readlines():
    newline = line.strip()
    newtext = newline.replace("jane", "jdoe")
    subprocess.run(['mv', '~/newline', '~/newtext']) #I know its wrong but this is my limit!

UPDATE

I modified the script to return list and comfortably iterate through its items, the script is successful but it is doing one iteration only which means it is applying only the first file name or the first iteration, any ideas to do all the iterations and finish the job. Thank you very much!!

#!/usr/bin/env python3
import sys
import subprocess
with open(sys.argv[1], 'r') as f:
    content1 = f.readlines()
    content2 = [x.strip() for x in content1]
    content3 = [item.replace("jane", "jdoe") for item in content2]
    for x in content2:
      for i in content3:
        subprocess.run(['mv', '/home/ninja-coder/'+x, '/home/ninja-coder/'+i])

Solution

  • Thanks to @Jonatã and @HackLab for their hints. After several trials I solved this very annoying assessment but I learned lots of things, again, one of the crucial success's secrets is PRACTICE, here is the final code

    #!/usr/bin/env python3
    import sys
    import subprocess
    with open(sys.argv[1], 'r') as f:
        content1 = f.readlines()
        content2 = [x.strip() for x in content1]
        content3 = [item.replace("jane", "jdoe") for item in content2]
        res = dict(zip(content2, content3))
        for key, value in res.items():
            subprocess.run(['mv', '/home/ninja-coder/'+key, '/home/ninja-coder/'+value])