0

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

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 each file's name is saved in one line.

Iterating and updating files names in oldFiles.txt is ok 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. Sorry for my bad explanation.

Actual output mv: cannot stat '/home/user/sdafsdfs_jane.doc 12fgdf-jane.csv files_jane.tx': No such file or directory From the output I can understand that maybe the path is getting attached only to the first newName and not to the rest of files names or it could be bash is reading all files names as one file name but I still don't know how to solve it.

PS: I am not showing my username and I am replacing it with user. So please change it into your current username.

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

Thank you very much for your support and your comprehension.

#!/usr/bin/env python3
import sys
import subprocess
with open(sys.argv[1]) as f:
    for name in f.readlines():
        newName = name.strip()
        lastName = newName.replace("jane", "jdoe")
        subprocess.run(["mv", "/home/user/"+newName, "/home/user/"+lastName])

UPDATE

I modified the script to return list so I can flawlessly iterate through items, actually the script is successfully applied on the first file name only while still 2 others, any idea please? 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])
  • What if you use os.rename instead? – Jonatã Jul 11 at 14:30
  • I am forced to use sys and subprocess modules only and to follow only the logic I described. Regards. – CYREX Jul 11 at 14:31
  • So, it is a homework exercise, we should not give you the answer :) but 2 tips: try to debug your script using simple print() to know what exactly is being sent to the mv command; and review mv command man to understand what it is expecting as arguments. – Jonatã Jul 11 at 14:39
  • Every line is successfully tested even the subprocess.run and I am not asking for solution, I am asking for hints. You could see that I built everything, still only this very annoying bug! – CYREX Jul 11 at 14:41
  • What if you print(name) right after the for name in f.readlines()? What is the output? – Jonatã Jul 11 at 14:46
0

Thanks to @Jonatã and @HackLab for their hints. After several debugging 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])
| improve this answer | |
0

I believe the code in this answer is confusing and difficult to understand. Here is a simpler version:


import sys
import subprocess

file = open(sys.argv[1])
for line in file:
        source = line.strip()
        dest = line.replace("jane", "jdoe")
        subprocess.run(['mv', '<homedirectorypath>'+source, '<homedirectorypath>'+dest])
file.close()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.