Python Forum
a challenging project idea
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a challenging project idea
#1
this is not a joke. it is currently in perl. the "rename" command that i use in linux, although making it work in windows is ok.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
https://docs.python.org/3/library/pathli...ath.rename

import pathlib

def rename(fname, to):
    pathlib.Path(fname).rename(to)

if __name__ == "__main__":
    import sys
    if len(sys.argv) >= 3:
        fname, to = sys.argv[1:3]
        rename(fname, to)
    else:
        print("rename.py: [from] [to]")
Reply
#3
it does not seem to work as a rename command:

Output:
lt2a/forums /home/forums 1> box rename.py +---<rename.py>---------------------------+ | import pathlib | | | | def rename(fname, to): | | pathlib.Path(fname).rename(to) | | | | if __name__ == "__main__": | | import sys | | if len(sys.argv) >= 3: | | fname, to = sys.argv[1:3] | | rename(fname, to) | | else: | | print("rename.py: [from] [to]") | +-----------------------------------------+ lt2a/forums /home/forums 2> ls -dl foo food /bin/ls: cannot access 'foo': No such file or directory /bin/ls: cannot access 'food': No such file or directory lt2a/forums /home/forums 3> touch foo lt2a/forums /home/forums 4> ls -dl foo food /bin/ls: cannot access 'food': No such file or directory -rw-r--r-- 1 forums forums 0 Apr 2 23:25 foo lt2a/forums /home/forums 5> py3 rename.py 's/foo/food' foo Traceback (most recent call last): File "rename.py", line 10, in <module> rename(fname, to) File "rename.py", line 4, in rename pathlib.Path(fname).rename(to) File "/usr/lib/python3.5/pathlib.py", line 1279, in rename self._accessor.rename(self, target) File "/usr/lib/python3.5/pathlib.py", line 377, in wrapped return strfunc(str(pathobjA), str(pathobjB), *args) FileNotFoundError: [Errno 2] No such file or directory: 's/foo/food' -> 'foo' lt2a/forums /home/forums 6>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
https://linux.die.net/man/1/rename

So it's more complicated than just an alias for mv. I didn't realize it was basically sed but for file names.
Reply
#5
that's what makes it challenging. the implementation in Ubuntu is in Perl. i figured if Python had PCRE support, then it might be doable.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Idea of project dibax10 2 2,061 Nov-26-2019, 08:57 PM
Last Post: dibax10
  a less challenging project idea Skaperen 2 2,355 Apr-07-2019, 03:01 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020