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
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)
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')
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
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.
(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
thanks every one
finally i resolve that
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') + '/')