i wrote a sorting script, but the artist is also found in the middle of the word
the structure looks like this :
/home/jail/user/archiv/MP3/MP3_sorted/Farid_Bang_Feat._Kollegah_und_Fler_-_PUBLIC_ENEMIES
/home/jail/user/archiv/MP3/MP3_Archiv/F/Fler/
/home/jail/user/archiv/MP3/MP3_Archiv/F/Farid/
but now will Farid_Bang_Feat.Kollegah_und_Fler-_PUBLIC_ENEM moved to the wrong directory.
i want it in /home/jail/user/archiv/MP3/MP3_Archiv/F/Farid/ and not in /home/jail/user/archiv/MP3/MP3_Archiv/F/Fler/
the artist must be fixed on the start of the word in the name. because Fler also occurs in the string
I tried ^ but it doesn't work
if re.search(^artist, name):
.
import os
import re
import shutil
main_path = '/home/jail/user/archiv/MP3/MP3_Archiv'
sort_path = '/home/jail/user/archiv/MP3/MP3_sorted'
def main():
dirlist = [item for item in os.listdir(main_path) if os.path.isdir(os.path.join(main_path, item))]
for dir in dirlist:
for dir2 in dir:
dir2 = main_path + '/' + dir2
dirlist2 = [item for item in os.listdir(dir2) if os.path.isdir(os.path.join(dir2, item))]
for artist in dirlist2:
print('artist', artist)
dirlist3 = [item for item in os.listdir(sort_path) if os.path.isdir(os.path.join(sort_path, item))]
for name in dirlist3:
chara = artist[:1]
source = sort_path + '/' + name
destination = main_path + '/' + chara + '/' + artist + '/'
if re.search(artist, name):
shutil.move(source, destination)
print('name moved to', destination + name)
main()
print('done')
You wrote
if re.search(artist, name):
Johnny Mopp explains that this implements your desired search criterion:
if re.search('^' + artist, name):
Related
I want to make a script that monitors changes in a folder and moves files to special directories. I use pip-watchdog
from watchdog.events import FileSystemEventHandler
def makeUnique(path,counter):
filename, extension = os.path.splitext(path)
# IF FILE EXISTS, ADDS NUMBER TO THE END OF THE FILENAME
file_split=filename.split('\\')
new_name="DUPLICATE_"+file_split[-1]
new_file=file_split[-1].replace(file_split[-1],new_name)
while os.path.exists(path):
path = new_file + " (" + str(counter) + ")" + extension
counter += 1
return path
def move(dest, entry, name):
"""dest = D:\Download\ChromeSetup.exe"""
file_exists = os.path.exists(dest + "\\" + name)
counter=1
if file_exists:
unique_name = makeUnique(entry,counter)
dest = dest+"\\"+unique_name
os.rename(entry, dest)
try:
print(f'[magenta][[/] [indian_red1 bold]{name}[/] [magenta]][/] -> [magenta][[/]
[orange_red1 bold]{dest}[/] [magenta]][/] to ' + f'[dark_orange]{dest}[/]')
shutil.move(entry,dest)
except:
pass
def moveByExtension(str, dest_dir, entry, name):
if name.endswith(str):
dest = dest_dir
move(dest, entry, name)
dest_dir_exe = r'D:\Download\exe_files'
class MoverHandler(FileSystemEventHandler):
def on_created(self, event):
"""when a file or directory is created"""
with os.scandir(source_dir) as entries:
for entry in entries:
name = entry.name
# app
moveByExtension('.exe', dest_dir_exe, entry, name)
My current filesystem tree:
-Download/
-- ChromeSetup.exe (rename and move to exe_files)
-Download/exe_files/
-- ChromeSetup.exe
-- DUPLICATE_ChromeSetup (1).exe
But if ChromeSetup.exe is in the download again, then the error occurs 'DUPLICATE_ChromeSetup (1).exe' already exists, although 'DUPLICATE_ChromeSetup (2).exe' is expected.
want to count if a file exists and store a duplicate followed by a copy number
I'm learning python at the moment...maybe I don't see the obvious problem
thank you in advance
I want to find folders that has no data in it and get thier folder names.
The first and second folders are named randomly in numbers and has data in random folders.
the codes are
path = 'M://done/mesh/*'
FL = glob.glob(path)
FL2 = glob.glob(FL[0] + '/*')
FL2
['M://done/mesh\\41\\23',
'M://done/mesh\\41\\24',
'M://done/mesh\\41\\33',
'M://done/mesh\\41\\34',
'M://done/mesh\\41\\35',
'M://done/mesh\\41\\36',
'M://done/mesh\\41\\43',
'M://done/mesh\\41\\44',
'M://done/mesh\\41\\45',
'M://done/mesh\\41\\46',
'M://done/mesh\\41\\47',
'M://done/mesh\\41\\53',
'M://done/mesh\\41\\54',
'M://done/mesh\\41\\55',
'M://done/mesh\\41\\63',
'M://done/mesh\\41\\64',
'M://done/mesh\\41\\65',
'M://done/mesh\\41\\66',
'M://done/mesh\\41\\67',
'M://done/mesh\\41\\74',
'M://done/mesh\\41\\75',
'M://done/mesh\\41\\76',
'M://done/mesh\\41\\77',
'M://done/mesh\\41\\85',
'M://done/mesh\\41\\86',
'M://done/mesh\\41\\87']
FL2[24][24:26] + FL2[24][27:30] + '0000' # why do I need [24:26}, [27:30]???
finding_files = ['_Caminfo.dat','running.csv']
print(FL2[0] + '/0000/02_output/' + FL2[0][24:26] + FL2[0][27:30] + '0000/' + fn1[0])
'41860000'
You can use os.listdir to find empty folders, like below:
import os
folder_list = ["D:\\test_1", "D:\\test_2"]
empty_folders = []
for folder in folder_list:
try:
if not os.listdir(folder):
empty_folders.append(folder)
except FileNotFoundError:
pass
print(empty_folders)
I use a python script to webscrape for "Show Notes" and an mp3. When I encounter a page that has no show notes, this means the show was a Best Of, so I want to skip the download of the notes and mp3. I am not sure where the best place to insert the test would be. The snippet is as follows:
for show_html in showpage_htmls:
try:
p_html = s.get(show_html)
p_soup = BeautifulSoup(p_html.content, 'html.parser')
# set title for SHOW NOTES
title = ''
title = p_soup.title.contents[0]
# get SHOW NOTES chunk and remove unwanted characters (original mp3notes not changed)
mp3notes = ''
mp3notes = p_soup.find('div', {'class': 'module-text'}).find('div')
mp3notes = str(title) + str('\n') + str(mp3notes).replace('<div>','').replace('<h2>','').replace('</h2>','\n').replace('<p>','').replace('<br/>\n','\n').replace('<br/>','\n').replace('</p>','').replace('</div>','').replace('\u2032','')
# FIXME need to skip d/l if no notes
# set basename, mp3named and mp3showtxt
mp3basename = '{0}{1}{2}'.format(show_html.split('/')[3],show_html.split('/')[4],show_html.split('/')[5])
if (os.name == 'nt'):
mp3showtxt = mp3dir + '\\' + mp3basename + '.txt'
mp3named = mp3dir + '\\' + mp3basename + '.mp3'
else:
mp3showtxt = mp3dir + '/' + mp3basename + '.txt'
mp3named = mp3dir + '/' + mp3basename + '.mp3'
# save show notes to local
with open(mp3showtxt, 'w') as f:
try:
f.write(mp3notes)
print("Show notes " + mp3basename + " saved.")
except UnicodeEncodeError:
print("A charmap encoding ERROR occurred.")
print("Show notes for " + mp3basename + ".mp3 FAILED, but continuing")
finally:
f.close()
# FIXME need eyed3 to set mp3 tags since B&T are lazy
# get Full Show mp3 link
mp3url = p_soup.find('a', href = True, string = 'Full Show').get('href')
# get and save mp3
r = requests.get(mp3url)
with open(mp3named, 'wb') as f:
f.write(r.content)
print("Downloaded " + mp3basename + ".mp3.")
except AttributeError:
print(show_html + " did not exist as named.")
I would think an
if not (len(mp3notes) >= 50)
would work; just not sure where to put it or there is better way (more Pythonic).
Ideally, if the mp3notes are less than expected, no notes or mp3 for that show_html would be saved, and the script would start at the next show_html page.
Since I am new to Python, feel free to offer suggestions to making this more Pythonic as well; I am here to learn! Thanks.
I'm trying to right a script that will walk through a given directory and rename, move, and delete the original directory. Each directory has a *.mp4 file and a subfolder containing *.jpg files. I'm having trouble deleting the original directory when I'm done renaming and moving the files. What so far is:
import datetime, os, re, shutil, logging, smtplib
logging.basicConfig(level = logging.INFO, format = ' %(asctime)s - %(levelname)s - %(message)s')
#logging.disable(logging.info)
path = '\\\\RT-N66U\\sda2\\Documents'
desired_text1 = re.compile(r'^\d{3}_[a-zA-Z]+\d{3}.jpg')
desired_text2 = re.compile(r'^\d{3}_[a-zA-Z]+_\d{3}.jpg')
renamed_files = ''
for folderName, subfolders, filenames in os.walk(path):
if folderName.startswith(path + '\\GEO-'):
logging.info('The current folder is ' + folderName)
for subfolder in subfolders:
if subfolder.startswith('Pics'):
pic_folder = subfolder
for filename in filenames:
if filename.endswith('.mp4'):
changed_date = datetime.datetime.strptime(filename[3:17], '%b %d, %Y').strftime('%y.%m.%d') # Changes the name of the date to the desired format
year = datetime.datetime.strptime(filename[3:17], '%b %d, %Y').strftime('%Y')
name = filename[20:-25]
name = name.lower()
modified_name = '.'.join(re.findall('\S+', name))
final_name = 'geo.' + changed_date + '.' + modified_name
if not os.path.exists('\\\\RT-N66U\\sda2\\Me\\GEO\\%s\\%s\\%s' % (year, changed_date[3:5], final_name)):
os.makedirs('\\\\RT-N66U\\sda2\\Me\\GEO\\%s\\%s\\%s' % (year, changed_date[3:5], final_name))
if not os.path.exists('\\\\RT-N66U\\sda2\\Me\\GEO\\%s\\%s\\%s\\%s' % (year, changed_date[3:5], final_name, pic_folder)):
os.makedirs('\\\\RT-N66U\\sda2\\Me\\GEO\\%s\\%s\\%s\\%s' % (year, changed_date[3:5], final_name, pic_folder))
if filename.endswith('.mp4'):
logging.info(filename)
os.rename(folderName + '\\' + filename, '\\\\RT-N66U\\sda2\\Me\\GEO\\%s\\%s\\%s\\%s.mp4' % (year, changed_date[3:5], final_name, final_name))
# This part will logg and rename the files by adding a '_'
mo1 = desired_text1.search(filename)
mo2 = desired_text2.search(filename)
if mo1:
logging.info(folderName + '\\' + filename)
os.rename(folderName + '\\' + filename, '\\\\RT-N66U\\sda2\\Me\\GEO\\%s\\%s\\%s\\%s\\%s_%s' % (year, changed_date[3:5], final_name, pic_folder, filename[:-7], filename[-7:]))
renamed_files += filename + ' -> ' + filename[:-7] + '_' + filename[-7:] + '\n'
elif mo2:
logging.info(folderName + '\\' + filename)
os.rename(folderName + '\\' + filename, '\\\\RT-N66U\\sda2\\Me\\GEO\\%s\\%s\\%s\\%s\\%s' % (year, changed_date[3:5], final_name, pic_folder, filename))
renamed_files += filename + '\n'
# This part will check if the directory is empty and delete it if so
if os.listdir(folderName) == []:
logging.info(folderName)
shutil.rmtree(folderName)
What ends up happening is that all sub-directories will be deleted if they're empty and leaving the primary directory empty. I would like the primary directory to be removed as well, since all the files have been moved. Is there a way to accomplish this within the for statement?
If you mean deleting the directory known as folderName inside your main for loop, I think it's not really possible since you're iterating over an object that contains it. Like if inside a for i in this_list: you would like to remove an object from this_list. This is not possible before you're out of the loop.
In order to achieve what you want, I would write a function to recurse into the folder tree. The idea would be something like:
def process_folder(folder_name):
do some stuff on files
get the subfolders of folder_name
for f in subfolders:
process_folder(subfolder_name)
if subfolder_name is empty, delete it
maybe do some other stuff on files
and in the main script, call process_folder(path). (This would leave only the top folder there even if it's empty though, so be sure you don't want to delete it; or to call process_folder at a higher level, or to add an instruction to delete it too if it's empty).
Hi I have recently made this script to rename files I scan for work with a prefix and a date. It works pretty well however it would be great if it could make a directory in the current directory with the same name as the first file then move all the scanned files there. E.g. First file is renamed to 'Scanned As At 22-03-2012 0' then a directory called 'Scanned As At 22-03-2012 0' (Path being M:\Claire\Scanned As At 22-03-2012 0) is made and that file is placed in there.
I'm having a hard time figuring out the best way to do this. Thanks in advance!
import os
import datetime
#target = input( 'Enter full directory path: ')
#prefix = input( 'Enter prefix: ')
target = 'M://Claire//'
prefix = 'Scanned As At '
os.chdir(target)
allfiles = os.listdir(target)
count = 0
for filename in allfiles:
t = os.path.getmtime(filename)
v = datetime.datetime.fromtimestamp(t)
x = v.strftime( ' %d-%m-%Y')
os.rename(filename, prefix + x + " "+str(count) +".pdf")
count +=1
Not quite clear about your requirement. If not rename the file, only put it under the directory, then you can use the following codes (only the for-loop of your example):
for filename in allfiles:
if not os.isfile(filename): continue
t = os.path.getmtime(filename)
v = datetime.datetime.fromtimestamp(t)
x = v.strftime( ' %d-%m-%Y')
dirname = prefix + x + " " + str(count)
target = os.path.join(dirname, filename)
os.renames(filename, target)
count +=1
You can check help(os.renames).