I have a lot of pictures in a paste following a pattern for the file name, they only differ in the file type which may be .jpg or .jpeg
For instance:
IMG-20211127-WA0027.jpg
IMG-20211127-WA0028.jpeg
IMG-20211127-WA0029.jpg
I'm trying to find a way to create a folder for each year and send the pictures for the respective folder, given that the file name already has its year.
How can I create folders for each year, move the files to the right folder?
I tried to adapt a code from a tutorial, but I'm not getting what I need.
Please see my code below :
from distutils import extension
import os
import shutil
path = "D:\WhatsApp Images"
files = os.listdir(path)
year = os.path.getmtime(path)
for file in files:
filename, extension = os.path.splitext(file)
extension = extension[1:]
if os.path.exists(path+'/'+extension):
shutil.move(path+'/'+file, path+'/'+extension+'/'+file)
else:
os.makedirs(path+'/'+extension)
shutil.move(path+'/'+file,path+'/'+extension+'/'+file)
You can try something like this: See the inline comments for an explanation.
from pathlib import Path
import shutil
import os
path = Path("D:\\WhatsApp Images") # path to images
for item in path.iterdir(): # iterate through images
if not item.suffix.lower() in [".jpg", "jpeg"]: # ensure each file is a jpeg
continue
parts = item.name.split("-")
if len(parts) > 1 and len(parts[1]) > 5:
year = parts[1][:4] # extract year from filename
else:
continue
if not os.path.exists(path / year): # check if directory already exists
os.mkdir(path / year) # if not create the directory
shutil.move(item, path / year / item.name) # copy the file to directory.
I like #alexpdev 's answer, but you can do this all within pathlib alone:
from pathlib import Path
path_to_your_images = "D:\\WhatsApp Images"
img_types = [".jpg", ".jpeg"] # I'm assuming that all your images are jpegs. Extend this list if not.
for f in Path(path_to_your_images).iterdir():
if not f.suffix.lower() in img_types:
# only deal with image files
continue
year = f.stem.split("-")[1][:4]
yearpath = Path(path_to_your_images) / year # create intended path
yearpath.mkdir(exist_ok = True) # make sure the dir exists; create it if it doesn't
f.rename(yearpath / f.name) # move the file to the new location
Related
I would like to do extract 7z file, first I've to checking whether the 7z file exist or not, do the extraction, then mapping a specific file in those extracted folder then move the extracted folder based on the result. If the file that I mapped exist on the folder, then move to valid folder otherwise move to invalid folder.
I have done some code, but still got a problem how to move the invalid folder.
Here is what I've done.
This is the config.py
RAW_DIR = "E:/data/raw"
OUTPUT_DIR = "E:/data/output"
RESULT_DIR = "E:/data/output/output"
VALID_DIR = "E:/data/valid"
INVALID_DIR = "E:/data/invalid"
RAW_IDENTIFIER = "E:/data/raw/*_zipfile.7z"
VALID_IDENTIFIER = "/system/result/processing.log"
This the main.py
import time
import glob
import shutil
import os.path
import config as cfg
from pyunpack import Archive
# Extract
for i, f in enumerate(glob.glob(os.path.join(cfg.RAW_DIR, cfg.RAW_IDENTIFIER))):
timestamp = time.strftime("%Y%m%d%H%M%S")
output_path = cfg.RESULT_DIR + str(i) + "_" + timestamp
os.mkdir(output_path)
Archive(f).extractall(output_path)
# Mapping
for folder in enumerate(glob.glob(output_path + cfg.VALID_IDENTIFIER)):
shutil.move(output_path, cfg.VALID_DIR)
file = glob.glob(cfg.OUTPUT_DIR + "/output*")
file_name = os.path.basename(file)
shutil.move(file_name, cfg.INVALID_DIR)
Anyone can help me please. Thank's a lot
Have a task where i have to sort files into groups using their time frames
Suppose i have files like “ 2018/12/12 11:32:34 xyz.txt “
And the task that i have is That I have to use python to first extract the timeframe from the files and order them into groups by creating directories firstly as per years then as per months and finally the all the files inside their respective years and months
Like for the file in above egs it should be in a path like
Files/2018/December/file.txt
Just need help regarding which libraries to use andnd how to approach the problem
So, you want to copy files with timestamp names to the directories. Our input data:
Directory with timestamp files
Directory, where we need to save our files.
We ask a user to input those directories, and after that, we need to open directory 1 (directory with timestamp files) and read each one step by step.
We can get all filenames in the directory and iterate in like in the list, parse date in filename. We get the first filename and split the filename by space. Now we have a date and a time in the first array element and in the second.
Now we will use the datetime library to transform date and time to datetime. After doing that we can easily get a year, month, day, etc.
Now we can check year and month and create a folder connected with that year and month. If a folder not exists, we can create it. After that - use copy to copy file in that folder
EDIT. My solution:
import glob
import os
from datetime import datetime
from shutil import copyfile
def getListOfFilenamesInFolder(src):
filenamesWithPath = glob.glob(f"{src}\\*.txt")
filenames = [filename.split('\\')[-1] for filename in filenamesWithPath]
return filenames
def parseFilename(filename):
splittedFilename = filename.split(' ')
dateFilename = splittedFilename[0]
timeFilename = splittedFilename[1]
datetimeFilename = datetime.strptime(f'{dateFilename} {timeFilename}', '%Y-%m-%d %H.%M.%S')
return datetimeFilename
def createFolderIfNotExist(dest, datetimeFilename):
path = os.path.join(dest, 'File')
if not os.path.exists(path):
os.mkdir(path)
path = os.path.join(path, str(datetimeFilename.year))
if not os.path.exists(path):
os.mkdir(path)
path = os.path.join(path, datetimeFilename.strftime("%b"))
if not os.path.exists(path):
os.mkdir(path)
return path
pass
def makeSortingByFilenames(src, dest):
listOfFilenames = getListOfFilenamesInFolder(src)
print(listOfFilenames)
for filename in listOfFilenames:
datetimeFilename = parseFilename(filename)
path = createFolderIfNotExist(dest, datetimeFilename)
copyfile(os.path.join(src, filename), os.path.join(path, ' '.join(filename.split(' ')[2:])))
if __name__ == '__main__':
srcDirectory = input()
destDirectory = input()
makeSortingByFilenames(srcDirectory, destDirectory)
I have that structure folder. If you have another filenames, you need to change datetimeFilename = datetime.strptime(f'{dateFilename} {timeFilename}', '%Y-%m-%d %H.%M.%S') to your specific filenames.
Screenshots:
My input and output:
Results:
I am aiming to create a function that does the following:
Declare a path with a file, not just a folder. e.g. 'C:/Users/Lampard/Desktop/Folder1/File.py'
Create a folder in same folder as the declared file path - Calling it 'Archive'
Cut the file and paste it into the new folder just created.
If the folder 'Archive' already exists - then simply cut and paste the file into there
I have spent approx. 15-20min going through these:
https://www.programiz.com/python-programming/directory
Join all except last x in list
https://docs.python.org/3/library/pathlib.html#operators
And here is what I got to:
import os
from pathlib import Path, PurePath
from shutil import copy
#This path will change every time - just trying to get function right first
path = 'C:/Users/Lampard/Desktop/Folder1/File.py'
#Used to allow suffix function
p = PurePath(path)
#Check if directory is a file not a folder
if not p.suffix:
print("Not an extension")
#If it is a file
else:
#Create new folder before last file
#Change working directory
split = path.split('/')
new_directory = '/'.join(split[:-1])
apply_new_directory = os.chdir(new_directory)
#If folder does not exist create it
try:
os.mkdir('Archive')#Create new folder
#If not, continue process to copy file and paste it into Archive
except FileExistsError:
copy(path, new_directory + '/Archive/' + split[-1])
Is this code okay? - does anyone know a simpler method?
Locate folder/file in path
print [name for name in os.listdir(".") if os.path.isdir(name)]
Create path
import os
# define the name of the directory to be created
path = "/tmp/year"
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s " % path)
To move and cut files you can use this library
As you're already using pathlib, there's no need to use shutil:
from pathlib import Path
path = 'C:/Users/Lampard/Desktop/Folder1/File.py' # or whatever
p = Path(path)
target = Path(p.with_name('Archive')) # replace the filename with 'Archive'
target.mkdir() # create target directory
p.rename(target.joinpath(p.name)) # move the file to the target directory
Feel free to add appriopriate try…except statements to handle any errors.
Update: you might find this version more readable:
target = p.parent / 'Archive'
target.mkdir()
p.rename(target / p.name)
This is an example of overloading / operator.
I only know how to write python for GIS purposes. There is more to this code using arcpy and geoprocessing tools.... this is just the beginning part I'm stuck on that is for trying to get data ready so I can then use the shapefiles within the zipped folder for the rest of my script
I am trying to prompt the user to enter a directory to search through. For use of this script it will be searching for a compressed zip file, then extract all the files to that same directory.
import zipfile, os
# ask what directory to search in
mypath = input("Enter .zip folder path: ")
extension = ".zip"
os.chdir(mypath) # change directory from working dir to dir with files
for item in os.listdir(mypath):
if item.endswith(extension):
filename = os.path.abspath(item)
zip_ref = zipfile.ZipFile(filename)
zip_ref.extractall(mypath)
zip_ref.close()
Tried with y'alls suggestions and still have issues with the following:
import zipfile, os
mypath = input("Enter folder: ")
if os.path.isdir(mypath):
for dirpath, dirname, filenames in os.listdir(mypath):
for file in filenames:
if file.endswith(".zip"):
print(os.path.abspath(file))
with zipfile.ZipFile(os.path.abspath(file)) as z:
z.extractall(mypath)
else:
print("Directory does not exist.")
I'm not sure on the use of arcpy. However...
To iterate over entries in a directory, use os.listdir:
for entry_name in os.listdir(directory_path):
# ...
Inside the loop, entry_name will be the name of an item in the directory at directory_path.
When checking if it ends with ".zip", keep in mind that the comparison is case sensitive. You can use str.lower to effectively ignore case when using str.endswith:
if entry_name.lower().endswith('.zip'):
# ...
To get the full path to the entry (in this case, your .zip), use os.path.join:
entry_path = os.path.join(directory_path, entry_name)
Pass this full path to zipfile.ZipFile.
Your first if-block will negate the else-block if the location is invalid. I'd remove the 'else' operator entirely. If you keep it, the if-check effectively kills the program. The "if folderExist" is sufficient to replace the else.
import arcpy, zipfile, os
# ask what directory to search in
folder = input("Where is the directory? ")
# set workspace as variable so can change location
arcpy.env.workspace = folder
# check if invalid entry - if bad, ask to input different location
if len(folder) == 0:
print("Invalid location.")
new_folder = input("Try another directory?")
new_folder = folder
# does the above replace old location and re set as directory location?
# check to see if folder exists
folderExist = arcpy.Exists(folder)
if folderExist:
# loop through files in directory
for item in folder:
# check for .zip extension
if item.endswith(".zip"):
file_name = os.path.abspath(item) # get full path of files
print(file_name)
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(folder) # extract all to directory
zip_ref.close() # close file
This may be neater if you're okay not using your original code:
import zipfile, os
from tkinter import filedialog as fd
# ask what directory to search in
folder = fd.askdirectory(title="Where is the directory?")
# loop through files in directory
for item in os.listdir(folder):
# check for .zip extension
if zipfile.is_zipfile(item):
file_name = os.path.abspath(item) # get full path of files
# could string combine to ensure path
# file_name = folder + "/" + item
print(file_name)
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(folder) # extract all to directory
zip_ref.close() # close file
[UPDATED]
I was able to solve the same with the following code
import os
import zipfile
mypath = raw_input('Enter Folder: ')
if os.path.isdir(mypath):
for file in os.listdir(mypath):
if file.endswith('.zip'):
with zipfile.ZipFile(os.path.join(mypath, file)) as z:
z.extractall(mypath)
else:
print('Directory does not exist')
I have been working this challenge for about a day or so. I've looked at multiple questions and answers asked on SO and tried to 'MacGyver' the code used for my purpose, but still having issues.
I have a directory (lets call it "src\") with hundreds of files (.txt and .xml). Each .txt file has an associated .xml file (let's call it a pair). Example:
src\text-001.txt
src\text-001.xml
src\text-002.txt
src\text-002.xml
src\text-003.txt
src\text-003.xml
Here's an example of how I would like it to turn out so each pair of files are placed into a single unique folder:
src\text-001\text-001.txt
src\text-001\text-001.xml
src\text-002\text-002.txt
src\text-002\text-002.xml
src\text-003\text-003.txt
src\text-003\text-003.xml
What I'd like to do is create an associated folder for each pair and then move each pair of files into its respective folder using Python. I've already tried working from code I found (thanks to a post from Nov '12 by Sethdd, but am having trouble figuring out how to use the move function to grab pairs of files. Here's where I'm at:
import os
import shutil
srcpath = "PATH_TO_SOURCE"
srcfiles = os.listdir(srcpath)
destpath = "PATH_TO_DEST"
# grabs the name of the file before extension and uses as the dest folder name
destdirs = list(set([filename[0:9] for filename in srcfiles]))
def create(dirname, destpath):
full_path = os.path.join(destpath, dirname)
os.mkdir(full_path)
return full_path
def move(filename, dirpath):
shutil.move(os.path.join(srcpath, filename)
,dirpath)
# create destination directories and store their names along with full paths
targets = [
(folder, create(folder, destpath)) for folder in destdirs
]
for dirname, full_path in targets:
for filename in srcfile:
if dirname == filename[0:9]:
move(filename, full_path)
I feel like it should be easy, but Python isn't something I work with everyday and it's been a while since my scripting days... Any help would be greatly appreciated!
Thanks,
WK2EcoD
Use the glob module to interate all of the 'txt' files. From that you can parse and create the folders and copy the files.
The process should be as simple as it appears to you as a human.
for file_name in os.listdir(srcpath):
dir = file_name[:9]
# if dir doesn't exist, create it
# move file_name to dir
You're doing a lot of intermediate work that seems to be confusing you.
Also, insert some simple print statements to track data flow and execution flow. It appears that you have no tracing output so far.
You can do it with os module. For every file in directory check if associated folder exists, create if needed and then move the file. See the code below:
import os
SRC = 'path-to-src'
for fname in os.listdir(SRC):
filename, file_extension = os.path.splitext(fname)
if file_extension not in ['xml', 'txt']:
continue
folder_path = os.path.join(SRC, filename)
if not os.path.exists(folder_path):
os.mkdir(folderpath)
os.rename(
os.path.join(SRC, fname),
os.path.join(folder_path, fname)
)
My approach would be:
Find the pairs that I want to move (do nothing with files without a pair)
Create a directory for every pair
Move the pair to the directory
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os, shutil
import re
def getPairs(files):
pairs = []
file_re = re.compile(r'^(.*)\.(.*)$')
for f in files:
match = file_re.match(f)
if match:
(name, ext) = match.groups()
if ext == 'txt' and name + '.xml' in files:
pairs.append(name)
return pairs
def movePairsToDir(pairs):
for name in pairs:
os.mkdir(name)
shutil.move(name+'.txt', name)
shutil.move(name+'.xml', name)
files = os.listdir()
pairs = getPairs(files)
movePairsToDir(pairs)
NOTE: This script works when called inside the directory with the pairs.