Python Forum
a challenging project idea - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: a challenging project idea (/thread-17203.html)



a challenging project idea - Skaperen - Apr-02-2019

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.


RE: a challenging project idea - nilamo - Apr-02-2019

https://docs.python.org/3/library/pathlib.html#pathlib.Path.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]")



RE: a challenging project idea - Skaperen - Apr-03-2019

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>



RE: a challenging project idea - nilamo - Apr-03-2019

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.


RE: a challenging project idea - Skaperen - Apr-04-2019

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.