Python Forum
Find all “*.wav” files that created yesterday on linux host with python.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find all “*.wav” files that created yesterday on linux host with python.
#1
There is so many .wav files and create alot of these files everyday on Linux Host. I want to find all wav files that created yesterday ( a day ago when script run )on subdirectories and convert them to mp3 format with python script. I run os.walk ( path ) for finding '.wav' files but can not check that they was create yesterday.
1
2
3
4
5
6
for root, dir, files in os.walk(path):
    for file in files:
        if file.endswith('.wav'):
            wav = root + '/' + str(file)
            cmd = 'lame --preset insane %s' % wav
            subprocess.call(cmd, shell=True)
Your answers can help alot.

Thanks in advance
Reply
#2
This will walk from starting path:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from pathlib import Path
 
 
def walk_dir(starting_dir):
    flist = []
    for path in Path(starting_dir).iterdir():
        if path.is_file():
            if path.suffix == '.wav':
                print(path)
                flist.append(path)
        elif path.is_dir():
            walk_dir(path)
 
    for file in flist:
        print(file)
 
 
if __name__ == '__main__':
    start_path = '/home/'
    walk_dir(start_path)
Reply
#3
Use os.stat() to check when files is created.
Make a yesterday date object and compare.
Example.
1
2
3
4
5
6
7
8
from datetime import date, timedelta
from pathlib import Path
 
path = Path('some.wav')
yesterday = date.today() - timedelta(days=1)
timestamp = date.fromtimestamp(path.stat().st_mtime)
if yesterday == timestamp:
    print('Do something file is from yesterday')
Reply
#4
tnx Larz60+ for your reply
i have a question , that my files are so much ( about 5-6 thousand of WAV files and about 25-30 GB ) . because of that if i save that file to a list , is there inefficient for using list?
sorry for my bad English

(Dec-29-2019, 06:57 PM)snippsat Wrote: Use os.stat() to check when files is created. Make a yesterday date object and compare. Example.
1
from datetime import date, timedelta from pathlib import Path path = Path('some.wav') yesterday = date.today() - timedelta(days=1) timestamp = date.fromtimestamp(path.stat().st_mtime) if yesterday == timestamp: print('Do something file is from yesterday')

Thanks snippsat
i have some randome folder on my path ( like /archive_sounds/Trunk/* and within this folders ( * ) , there is WAV files. by this how can i use path for finding these files.

thanks so much
Reply
#5
pydeev Wrote:is there inefficient for using list?
Appending 6000 times to a list will cost you a fraction of a millisecond for the whole process. This is negligible as compared to the time needed to scan the file system with stat calls.
Reply
#6
(Jan-01-2020, 09:32 AM)pydeev Wrote: i have some randome folder on my path ( like /archive_sounds/Trunk/* and within this folders ( * ) , there is WAV files. by this how can i use path for finding these files.
You do it bye combine code you have gotten til now.
Example mine and @Larz60+,can also trow in subprocess call as you have in first code.
Here doing all in loop,so not saving to a list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from pathlib import Path
from datetime import date, timedelta
import subprocess
from os import fspath
 
def walk_dir(starting_dir):
    #flist = []
    yesterday = date.today() - timedelta(days=66)
    for path in Path(starting_dir).iterdir():
        timestamp = date.fromtimestamp(path.stat().st_mtime)
        if path.is_file() and yesterday == timestamp:
            print(fspath(path))
            out = subprocess.run(['ls', '-l', fspath(path)], capture_output=True)
            print(out.stdout.decode())
            #print(path)
 
        if path.is_dir():
            walk_dir(path)
 
if __name__ == '__main__':
    start_path = r'E:\div_code\sound_folder'
    walk_dir(start_path)
Output:
E:\div_code\sound_folder λ python find_files.py E:\div_code\sound_folder\hello_vs.txt -rw-r--r-- 1 Tom 197121 5 Oct 27 01:49 E:\div_code\sound_folder\hello_vs.txt E:\div_code\sound_folder\Ny mappe\in_folder.txt -rw-r--r-- 1 Tom 197121 15 Oct 27 01:58 E:\div_code\sound_folder\Ny mappe\in_folder.txt
So here will find all files .txt could of course be .wav 66-days old in sound_folder*(also files in all sub-folders).
If files is in this case are 66-days old run:
1
ls -l filename   # Displays size, data of the specified file
Reply
#7
thanks every one
finally i resolve that Dance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def walk_dir(starting_dir):
    flist = []
    yesterday = datetime.date.today() - datetime.timedelta(days=0)
    for path in pathlib.Path(starting_dir).iterdir():
        timestamp = datetime.date.fromtimestamp(path.stat().st_mtime)
        if path.is_dir() and yesterday == timestamp:
            print(" We Are Here : ", path)
            for test in pathlib.Path(path).iterdir():
                print("hiiiiiiiiiiiiiiiiiiiiiiiiiii", test)
                if test.suffix == '.wav':
                    flist.append(test)
 
    print(flist)
    for file in flist:
        subprocess.call('lame --preset insane %s' % file, shell=True)
    for pathh in pathlib.Path(starting_dir).iterdir():
        timestampp = datetime.date.fromtimestamp(pathh.stat().st_mtime)
        if pathh.is_dir() and yesterday == timestampp:
            for tes in pathlib.Path(pathh).iterdir():
                print("We Are Here Again : ", pathh)
                if str(pathh) == str(TEST_DIR + yesterdays.strftime('%Y-%m-%d')):
                    break
                if tes.suffix == '.mp3':
                    print("This Is MP3 File : ", tes)
                    # t = str(pathh.abspath(os.path.join(starting_dir, tes)))
                    mine = os.path.abspath(tes)
                    shutil.move(mine, TEST_DIR + yesterdays.strftime('%Y-%m-%d') + '/')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have created a bot in python but getting issue in element selection domic33 0 461 Mar-16-2025, 01:55 PM
Last Post: domic33
  [SOLVED] [Linux] Run Python script through cron? Winfried 2 1,227 Oct-19-2024, 06:29 PM
Last Post: Winfried
  Linux, Python, C ++, Brain2 and errors. PiotrBujakowski 0 983 Jun-24-2024, 03:41 PM
Last Post: PiotrBujakowski
  Is possible to run the python command to call python script on linux? cuten222 6 2,358 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Find duplicate files in multiple directories Pavel_47 9 7,049 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  How to use a variable in linux command in python code? ilknurg 2 2,398 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  where to host my python script tomtom 1 1,803 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  count every 28 files and find desire files RolanRoll 3 3,047 Dec-12-2021, 04:42 PM
Last Post: Axel_Erfurt
  Have to use Python 2.x to find "yesterday' files tester_V 6 4,097 Sep-19-2021, 12:26 AM
Last Post: tester_V
  Failing to connect to a host with WMI tester_V 6 6,156 Aug-10-2021, 06:25 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

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