In below program I am trying to rename files present in my directory. However each time I run different number of files are renamed and program stops without any error message.
'''
Created on 08-Jul-2017
#author: Pranav
'''
import os
import re
from shutil import move
class MyPatterns:
brackets = "([\(\[]).*?([\)\]])"
extra_spaces_pattern = " +"
class MyConstants:
#dir_path = "D:\Other\Books\Knowledge"
space = ' '
underscore = '_'
blank = ''
def resolve_filenames(dir_path,filename):
new_file_name = re.sub(MyPatterns.brackets,MyConstants.blank,filename)
new_file_name = re.sub(MyPatterns.extra_spaces_pattern,MyConstants.space,new_file_name)
new_file_name = new_file_name.replace(MyConstants.underscore, MyConstants.space)
new_file_name = new_file_name.title()
move(os.path.join(dir_path,filename),os.path.join(dir_path, new_file_name))
base_path = "D:\Other\Books"
directories = os.listdir(base_path)
print('Starting process to resolve file names from base path : {}: '.format(base_path))
for d in directories:
dir_path = os.path.join(base_path, d)
print('Resolving files for directory : {} '.format(d))
for i,fname in enumerate(os.listdir(dir_path)):
print(i,fname)
resolve_filenames(dir_path, fname)
Related
I have this error when I'm trying to run this code, maybe anyone can help me and give me directions.
After that problem will be solved, I need to create a loop that searching in all the xml files and finding a string, if that sting is in the file than it will move the file to the directory, anyone have an idea how can I do it?
import os
import glob
import shutil
def remove_ext(list_of_pathnames):
"""
removes the extension from each filename
"""
return [os.path.splitext(filename)[0] for filename in list_of_pathnames]
path = os.getcwd()
os.chdir("D:\\TomProject\\Images\\")
os.mkdir("Done\\") # create a new folder
newpath = os.path.join("D:\\TomProject\\","image_with_xml") # made it os independent...
list_of_jpgs = glob.glob(path+"*.jpg")
list_of_xmls = glob.glob(path+"*.xml")
list_of_txts = glob.glob(path+"*.txt")
print(list_of_jpgs, "\n\n", list_of_xmls, "\n\n", list_of_txts) #remove
jpgs_without_extension = remove_ext(list_of_jpgs)
xmls_without_extension = remove_ext(list_of_xmls)
txts_without_extension = remove_ext(list_of_txts)
for filename in jpgs_without_extension:
if filename in xmls_without_extension:
if filename in txts_without_extension:
print("moving", filename) #remove
shutil.move(filename + '*.jpg', newpath) # move image to new path.
shutil.move(filename + '*.xml', newpath)
shutil.move(filename + '*.txt', newpath)
make sure all jpg, xml, txt are in the current working directory. you may add one line to check current directory with
print(path)
please change the lines
list_of_jpgs = glob.glob(path+"*.jpg")
list_of_xmls = glob.glob(path+"*.xml")
list_of_txts = glob.glob(path+"*.txt")
to
list_of_jpgs = glob.glob(os.path.join(path,"*.jpg"))
list_of_xmls = glob.glob(os.path.join(path,"*.xml"))
list_of_txts = glob.glob(os.path.join(path,"*.txt"))
last three lines should be indented. and * should be removed.
for filename in jpgs_without_extension:
if filename in xmls_without_extension:
if filename in txts_without_extension:
print("moving", filename) #remove
shutil.move(filename + '.jpg', newpath) # move image to new path.
shutil.move(filename + '.xml', newpath)
shutil.move(filename + '.txt', newpath)
I just start python and i have to compare filename with folder name to launch the good sh script. (i'm using airflow)
import glob
import os
import shutil
from os import path
odsPath = '/apps/data/02_ODS/'
receiptPath = '/apps/data/80_DATA/01_Receipt/'
for files in os.listdir(receiptPath):
if(files.startswith('MEM_ZMII') or files.startswith('FMS') and files.endswith('.csv')):
parsedFiles = files.split('_')
pattern = '_'.join(parsedFiles[0:2])
fileName = '_'.join(parsedFiles[2:5])
fileName = fileName.split('-')[0].lower()
# print('appCode: ', pattern)
# print('fileName: ', fileName)
for odsFolder in os.listdir(odsPath):
if(odsFolder == fileName):
print('it exist: ', str(fileName))
else:
print('it\'s not')
I got 3 files in receiptPath , it only matching for 1 file, but not the others. Can someone help me?
Thank a lot!
Ok, your problem is that you overwrite your variable fileName, so at the end of the first for loop, it only keeps the last value, which is material_makt. The solution consists in saving all the filenames in a list fileNames_list, and then you can check if (odsFolder in fileNames_list) :
import glob
import os
import shutil
from os import path
odsPath = '/apps/data/02_ODS/'
receiptPath = '/apps/data/80_DATA/01_Receipt/'
fileNames_list = []
for files in os.listdir(receiptPath):
if(files.startswith('MEM_ZMII') or files.startswith('FMS') and files.endswith('.csv')):
parsedFiles = files.split('_')
pattern = '_'.join(parsedFiles[0:2])
fileName = '_'.join(parsedFiles[2:5])
fileName = fileName.split('-')[0].lower()
fileNames_list.append(fileName)
for odsFolder in os.listdir(odsPath):
if (odsFolder in fileNames_list):
print('it exist:', str(odsFolder))
else:
print('it\'s not')
Output :
it exist: zcormm_familymc
it exist: kpi_obj_data
it exist: material_makt
I have a small problem with a tool I built in Python.
This tool works classifying files by filenames and making folders by a word in every filename and then moving all the files to the corresponding folder.
Files:
09052019_6_filetype1_currenttime_randomnumber.xml
09052019_2_filetype2_currenttime_randomnumber.xml
09052019_9_filetype3_currenttime_randomnumber.xml
09052019_1_filetype3_currenttime_randomnumber.xml
09052019_1_filetype3_currenttime_randomnumber.xml
Actual results:
filetype1_Status_6(folder)
09052019_6_filetype1_currenttime_randomnumber.xml
filetype2_Status_2(folder)
09052019_2_filetype2_currenttime_randomnumber.xml
filetype3_Status_9(folder)
09052019_9_filetype3_currenttime_randomnumber.xml
filetype3_Status_1(folder)
09052019_1_filetype3_currenttime_randomnumber.xml
09052019_1_filetype3_currenttime_randomnumber.xml
Code Version 1.0
#!/usr/bin/python3
# v1.0
# Importing modules
import os
import shutil
import sys
# Path of input and output files
src = input('Input files: ')
dest = input('Destination files: ')
os.chdir(dest)
def classify():
for f in os.listdir(src):
splitname = f.split('_')
status = splitname[1]
topic = splitname[2]
foldername = topic + '_' + 'Status_' + status
if not os.path.exists(foldername):
os.mkdir(foldername)
shutil.move(os.path.join(src, f), foldername)
print('Sorting out files, please wait...')
classify()
print('¡DONE!')
Improvement
But in the v2.0 I would like to "improve" it a little more, just keeping the same usability but changing filenames from original name to "Message_*.xml" and it works but only moving one file, not all of them.
Current results:
filetype1_Status_6(folder)
Message_.xml
filetype2_Status_2(folder)
Message.xml
filetype3_Status_9(folder)
Message_.xml
filetype3_Status_1(folder)
Message_.xml
Expected results:
filetype1_Status_6(folder)
Message_.xml
filetype2_Status_2(folder)
Message.xml
filetype3_Status_9(folder)
Message_.xml
filetype3_Status_1(folder)
Message_.xml
Message_1.xml
Code Version 2.0
#!/usr/bin/python3
# v2.0
# Importing modules
import os
import shutil
import sys
# Path of input and output files
src = input('Input files: ')
dest = input('Destination files: ')
os.chdir(dest)
def classify():
for f in os.listdir(src):
splitname = f.split('_')
status = splitname[1]
topic = splitname[2]
foldername = topic + '_' + 'Status_' + status
newFileName = foldername + '\\' + 'Message_' + '.xml'
if not os.path.exists(foldername):
os.mkdir(foldername)
shutil.copy(os.path.join(src, f), newFileName)
print('Sorting out files, please wait...')
classify()
print('¡DONE!')
You are naming everything Message_ so you will never get multiple files. You need to parse the names in the folder and then increment the filenames accordingly.
msgName = 'Message_0'
newFileName = foldername + '\\' + msgName + '.xml'
if not os.path.exists(foldername):
os.mkdir(foldername)
else:
while os.path.isfile(newFileName) is True:
msgInt = int(msgName[8:])
msgInt += 1
msgName = msgName[:8] + str(msgInt)
newFileName = foldername + '\\' + msgName + '.xml'
shutil.copy(os.path.join(src, f), newFileName)
Now if you already have message_0.xml in your folder, you will get a message_1.xml instead, and so on.
This piece of code is my first attempt at creating a program. I'm getting an error when running it that reads:
PermissionError: [WinError 32] The process cannot access the file
because it is being used by another process:
'C:\Users\gabri\Desktop\' -> 'C:\Users\gabri\Desktop\Planilhas
Excel\'
What am I doing wrong? The goal of this program is to get all excel, then pdf, then word files and put them in folders created by the program.
import os
from glob import glob
# import cx_Freeze
print("Digite o diretório de origem.")
dirOrigem = input()
os.chdir(dirOrigem)
excel_files = glob('*.xlsx')
excel_files.append(''.join(glob('*.xls')))
dirDestinoXL = dirOrigem + '\\' + 'Planilhas Excel'
if not os.path.exists(dirDestinoXL):
os.makedirs(dirDestinoXL)
for i in excel_files:
os.rename(f'{dirOrigem}\\{"".join(i)}', f'{dirDestinoXL}\\{"".join(i)}')
os.chdir(dirOrigem)
pdf_files = glob('*.pdf')
dirDestinoPDF = dirOrigem + '\\' + 'PDF'
if not os.path.exists(dirDestinoPDF):
os.makedirs(dirDestinoPDF)
for p in pdf_files:
os.rename(f'{dirOrigem}\\{"".join(p)}', f'{dirDestinoPDF}\\{"".join(p)}')
os.chdir(dirOrigem)
word_files = glob('*.doc')
word_files.append(glob('*.docx'))
dirDestinoWord = dirOrigem + '\\' + 'Word'
if not os.path.exists(dirDestinoWord):
os.makedirs(dirDestinoWord)
for d in word_files:
os.rename(f'{dirOrigem}\\{"".join(d)}', f'{dirDestinoWord}\\{"".join(d)}')
I tried your program and it doesn't work as it is on my computer. I changed some lines and it works. Hope it helps
import os
from glob import glob
dirOrigem = r'C:\Users\fchal\Desktop\temp' # here I changed the code just because I didn't want to bother using input()
os.chdir(dirOrigem)
excel_files = glob('*.xlsx')
excel_files.extend(glob('*.xls'))
dirDestinoXL = dirOrigem + '\\' + 'xlsfile'
if not os.path.exists(dirDestinoXL):
os.makedirs(dirDestinoXL)
for i in excel_files:
os.rename(i, os.path.join(dirDestinoXL, i))
# same procedure for pdf and word files
I know that glob can be a mess sometimes. And if the files are open, you can get errors. Here's what I would do:
import os
def move_files_with_extension(from_dir, to_dir, *extensions):
if not os.path.isdir(from_dir):
raise ValueError('{} is not a real directory'.format(from_dir))
elif not os.path.isdir(to_dir):
raise ValueError('{} is not a real directory'.format(to_dir))
files_with_extensions = all_files_with_extensions_in(from_dir, *extensions)
for file_path in files_with_extensions:
os.rename(file_path, os.path.join(to_dir, os.path.basename(file_path)))
def all_files_with_extensions_in(dir, *extensions):
files_with_extensions = list()
for dir_path, dir_names, file_names in os.walk(dir):
for file_name in file_names:
if file_name.endswith(extensions):
files_with_extensions.append(os.path.join(dir_path, file_name))
return files_with_extensions
and then you can do:
dirOrigem = input()
excel_location = os.path.join(dirOrigem, 'Planilhas Excel')
move_files_with_extension(dirOrigem, excel_location, '.xls', '.xlsx')
and so on
This is one of my first python projects, and i'm trying to make a script that would write a script which can re-create the src/ directory. this would be what i distribute to users. It uses walk, and writes a python file that first creates all the directories, and then writes the files. The issue i have is making the the files into a single string that i can write to a file.
This is the program i have:
import os
import pickle
src = os.path.dirname(os.path.realpath(__file__)) + os.sep + 'src'
fPack = 'import os \nimport pickle \nmyDir = os.path.dirname(os.path.realpath(__file__))'
Pack =''
print 'Packing ' + src
pickle
for root, dirs, files in os.walk(src, topdown=True):
for name in files:
print os.path.join(root, name)
f = open(os.path.join(root, name), 'r')
Pack = Pack + '\nf = open(os.path.join(myDir,\'' + name + '\'), \'w\')'
fileCont = pickle.dumps(f.read())
Pack = Pack + '\nf.write(pickle.loads(\'' + fileCont + '\'))'
for name in dirs:
print os.path.join(root, name)
fPack = fPack + '\nos.makedirs(os.path.join(myDir,\'' + name + '\'))'
print '==================================================\n\n\n'
print fPack + Pack
f = open(os.getcwd() + os.sep + 'dist' + os.sep + 'Pack.py', 'w')
f.write(fPack)
f.write(Pack)
And if i run it in a directory with on subdirectory, and on file inside it creates this file
import os
import pickle
myDir = os.path.dirname(os.path.realpath(__file__))
os.makedirs(os.path.join(myDir,'SphereText'))
f = open(os.path.join(myDir,'TextMain.py'), 'w')
f.write(pickle.loads('S"########################################################\n#Main SphereText file. #\n#SpereText is a simple Notepad-Like plain text editor #\n########################################################\n\nfrom Tkinter import *\nfrom tkFileDialog import *\nimport tkSimpleDialog\n\nroot = Tk()\nroot.title('SphereText')\n\ndef fSave():\n fileName = asksaveasfilename(parent=root)\n f = open(fileName, 'w')\n f.write(text.get(1.0,END))\n\ndef fOpen():\n fileName = ''\n fileName = askopenfilename(parent=root)\n f = open(fileName, 'r')\n text.delete(1.0,END)\n text.insert(1.0, f.read())\n\ndef tReplace():\n Old = tkSimpleDialog.askstring('SphereText', 'Replace:')\n print Old\n New = tkSimpleDialog.askstring('SphereText', 'With:')\n print New\n content = text.get(1.0,END)\n content = content.replace(Old, New)\n text.delete(1.0,END)\n text.insert(1.0, content)\n \nmenubar = Menu(root)\nmenubar.add_command(label='Save', command=fSave)\nmenubar.add_command(label='Open', command=fOpen)\nmenubar.add_command(label='Replace', command=tReplace)\nroot.config(menu=menubar)\n\ntext = Text(root, wrap=WORD)\n\ntext.pack()\n\nroot.mainloop()\n"
p0
.'))
The 's aren't escaped, and there are two line breaks at the end. i thought that the whole point of serializing was that you could always read it back the same way. Anyone know how i can mak the file a valid string?
Sorry about the newbish question, i just found out i had been trying to reinvent the wheel. apparently, that already exists under the name Squeeze.