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.
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:
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.
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.
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.
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:
ls -l filename   # Displays size, data of the specified file
Reply
#7
thanks every one
finally i resolve that Dance

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
  Is possible to run the python command to call python script on linux? cuten222 6 634 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Find duplicate files in multiple directories Pavel_47 9 2,930 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  where to host my python script tomtom 1 1,233 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  count every 28 files and find desire files RolanRoll 3 2,019 Dec-12-2021, 04:42 PM
Last Post: Axel_Erfurt
  Have to use Python 2.x to find "yesterday' files tester_V 6 2,772 Sep-19-2021, 12:26 AM
Last Post: tester_V
  Failing to connect to a host with WMI tester_V 6 4,280 Aug-10-2021, 06:25 PM
Last Post: tester_V
  Python syntax in Linux St0rmcr0w 2 44,883 Jul-29-2021, 01:40 PM
Last Post: snippsat
  Logstash - sending Logstash messages to another host in case of Failover in python Suriya 0 1,642 Jul-27-2021, 02:02 PM
Last Post: Suriya
  find 'yesterdays files' tester_V 8 3,743 Jun-18-2021, 02:10 AM
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