Python Forum
How does pathlib.Path.rename work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does pathlib.Path.rename work?
#1
I want to write a small script for a friend who has a bunch of images restored after deletion ( 50 gigs ) and I'm trying to use the pathlib module for this. But the renamed files are moved into the working directory. How does rename method work?

Here is the initial code:

path = Path(args.path)
files = (f for f in path.rglob('*') if f.is_file())

for f in files:
    ext = f.suffix
    if ext != '.py':
        stats = f.stat()
        name = Path(datetime.fromtimestamp(stats.st_mtime).strftime('%Y%m%d-%H%M%S'))
        f.rename(name)
The directory contains two more with the images inside them. When I run the code all images are renamed and moved to the working directory ( the root scan directory for example. The default path is '.').
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
How does pathlib.Path.rename work? - by wavic - Aug-01-2018, 09:45 PM
RE: How does pathlib.Path.rename work? - by Larz60+ - Aug-01-2018, 10:06 PM
RE: How does pathlib.Path.rename work? - by wavic - Aug-01-2018, 10:28 PM
RE: How does pathlib.Path.rename work? - by wavic - Aug-02-2018, 08:12 AM
RE: How does pathlib.Path.rename work? - by Larz60+ - Aug-02-2018, 08:29 AM
RE: How does pathlib.Path.rename work? - by wavic - Aug-02-2018, 10:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why wont this path work one way, but will the other way? cubangt 2 693 Sep-01-2023, 04:14 PM
Last Post: cubangt
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,878 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Pathlib import not working chriswrcg 9 3,810 May-29-2022, 07:37 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,239 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  deleting an empty Dir, using pathlib.Path tester_V 9 5,929 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,534 Jun-22-2021, 04:15 AM
Last Post: tester_V
  pathlib destpath.exists() true even file does not exist NaN 9 4,746 Dec-01-2020, 12:43 PM
Last Post: NaN
  Question about if with () or without () / pathlib Tecuma 3 2,248 Apr-02-2020, 10:02 AM
Last Post: Tecuma
  pathlib hanging bluefrog 2 3,157 Sep-25-2018, 12:59 PM
Last Post: volcano63
  pathlib: resolving a path that does not exist Skaperen 6 5,566 Sep-08-2018, 12:25 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