Python Forum
Convert string to path using Python 2.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert string to path using Python 2.7
#1
Greetings!
I need to find yesterday's modified directories.

but a PC I'm running the code on has Python 2.7 and I cannot use pathlib...
The code I have has a problem (see the error below), I can't get a Path from the string..
"AttributeError: 'str' object has no attribute 'stat'"

I Googled for an answer for about an hour or so but could not find a solution... Confused
Here is the code I got so far:
import os.path
import datetime as dt

tz ='C:\\01'
for dr_name in os.listdir(tz):
    dr_path = os.path.join(tz, dr_name)
    print(type(dr_path)) ## <-- it is alredy a string not Path
    if os.path.isdir(dr_path): 
        print("Directory path name:", dr_path)
        print(type(dr_path))        
        np = os.path.normpath(dr_path) 
        print(type(np))  ## <-- Still string
        yesterday = dt.datetime.now().date() - dt.timedelta(days=1)
        if yesterday == dt.datetime.fromtimestamp(np.stat().st_mtime).date():
            print(f"  Directory modified yesterday -> {np}")
Thank you!
Reply


Messages In This Thread
Convert string to path using Python 2.7 - by tester_V - Nov-08-2021, 09:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,914 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  how to convert tuple value into string mg24 2 2,342 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  -i option changes sys.path (removes leading empty string '') markanth 6 1,988 Aug-26-2022, 09:27 PM
Last Post: markanth
  Convert string to float problem vasik006 8 3,407 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,525 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,408 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,476 Feb-13-2022, 04:50 PM
Last Post: menator01
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,212 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Convert each element of a list to a string for processing tester_V 6 5,324 Jun-16-2021, 02:11 AM
Last Post: tester_V
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,462 Apr-27-2021, 10:35 AM
Last Post: masterAndreas

Forum Jump:

User Panel Messages

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