How to create folder tree from list with paths - python-3.x

I am trying to put every path of a folder structure I need to be created into a list and then create all of them with os.makedirs() but something goes wrong. Only the Head-Folders are created, not the Sub-Folders.
def output_folders(trcpaths):
#trcpath is a list with several paths, example: ['/home/usr/folder1', '/home/usr/folder2']
global outputfolders
outputfolders = []
#Create Paths
for x, j in enumerate(trcpaths):
for i in os.listdir(trcpaths[x]):
if i.endswith('trc'):
folderpath1 = (j + '/' + i).split('.')[0] #/home/usr/folder1/outputfolder
folderpath2 = folderpath1 + '/Steps' #/home/usr/folder1/outputfolder/Steps
folderpath3 = folderpath2 + '/Step_1' #/home/usr/folder1/outputfolder/Steps/Step_1
folderpath4 = folderpath2 + '/Step_2'
folderpath5 = folderpath2 + '/Step_3'
folderpath6 = folderpath2 + '/Step_4'
folderpath7 = folderpath2 + '/Threshold'
outputfolders.append(folderpath1)
outputfolders.append(folderpath2)
outputfolders.append(folderpath3)
outputfolders.append(folderpath4)
outputfolders.append(folderpath5)
outputfolders.append(folderpath6)
outputfolders.append(folderpath7)
#Create Folders
for j, i in enumerate(outputfolders):
print(i)
if os.path.exists(i):
if j == 0:
input('The Output-Folder already exists! Overwrite?' )
shutil.rmtree(i)
os.makedirs(i)
Although when I print(i) the right folderpaths are printed but only the "Head-Folderpaths" are created like /home/usr/folder1/outputfolder and not all subsequent folderpaths. Why so?
This is what I get:
/home/usr/folder1/outputfolder
/home/usr/folder2/outputfolder
But this is what I need:
/home/usr/folder1/outputfolder
/home/usr/folder1/outputfolder/Steps
/home/usr/folder1/outputfolder/Steps/Step_1
/home/usr/folder1/outputfolder/Steps/Step_2
/home/usr/folder1/outputfolder/Steps/Step_3
/home/usr/folder1/outputfolder/Steps/Step_4
/home/usr/folder1/outputfolder/Steps/Threshold
/home/usr/folder2/outputfolder
/home/usr/folder2/outputfolder/Steps
/home/usr/folder2/outputfolder/Steps/Step_1
/home/usr/folder2/outputfolder/Steps/Step_2
/home/usr/folder2/outputfolder/Steps/Step_3
/home/usr/folder2/outputfolder/Steps/Step_4
/home/usr/folder2/outputfolder/Steps/Threshold

to keep your logic and your coding, with this code:
for j, i in enumerate(outputfolders):
print(i)
if os.path.exists(i):
if j == 0:
input('The Output-Folder already exists! Overwrite?' )
shutil.rmtree(i)
os.makedirs(i)
you dont create folder..you only delete the existing folder and recreate if it already exists
i'll add else to complete the operation:
for j, i in enumerate(outputfolders):
print(i)
if os.path.exists(i):
if j == 0:
input('The Output-Folder already exists! Overwrite?' )
shutil.rmtree(i)
os.makedirs(i)
else:
os.makedirs(i)

Try this (Tested on my Windows machine but should work on Linux as well)
import os
NUM_OF_STEPS = 5
def make_output_folders(trc_paths):
output_folders = []
for idx, path in enumerate(trc_paths):
for leaf in os.listdir(path):
if leaf.endswith('trc') and os.path.isdir(os.path.join(path, leaf)):
trc_folder = os.path.join(path, leaf)
output_folders.append(os.path.join(trc_folder, 'output_folder', 'Steps'))
steps_folder = output_folders[-1]
for x in range(1, NUM_OF_STEPS):
output_folders.append(os.path.join(steps_folder, 'Step_{}'.format(x)))
output_folders.append(os.path.join(trc_folder,'output_folder', 'Threshold'))
for _path in output_folders:
print(_path)
if not os.path.exists(_path):
os.makedirs(_path)
output_folders = []
# 'folder_1' contains a sub folder named '1_trc'
# 'folder_2' contains a sub folder named '2_trc'
make_output_folders(['c:\\temp\\55721430\\folder1', 'c:\\temp\\55721430\\folder2'])

Related

want to count if a file exists and store a duplicate followed by a copy number on python

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

Find folders that has no data in it and get their folder names

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)

ImportError: cannot import name 'count_hi' from 'countstring'

I have two .py files. When I try to run starting.py it just says "ImportError: cannot import name 'count_hi' from 'countstring' (C:\Users\suspended.mirror\PycharmProjects\blankpage\venv\countstring.py)". How can I get it to run?
I've tried various variations of renaming the import, adding .py, ensuring both .py files are in the same directory but still wrong apparently.
starting.py
from countstring import count_hi
count_hi("testhi")
countstring.py
class countstring:
def count_hi(str):
k = 0
i = 0
n = 0
is_hi = ""
hi_count = 0
while k < (len(str) - 1):
first_letter = str[0 + i]
second_letter = str[1 + n]
is_hi = first_letter + second_letter
i += 1
n += 1
k += 1
if is_hi == "hi":
hi_count += 1
print(hi_count)
Python needs to know which directory that module is in. Try using a relative import:
from .countstring import count_hi
Hope this helps!
I actually figured it out myself.
The second module countstring.py didn't need the "class countstring:" at the top. I don't know why that is but possibly because that module wasn't creating an object. It was just defining a function.

Printing output only once inside the loop

for i in os.listdir(path_1):
for j in os.listdir(path_2):
file_name = (j.split('.')[0])
if i.__contains__(file_name) and i.endswith('txt'):
txt_tym = os.path.getctime(path_1 + '/' + i)
log_tym = os.path.getctime(path_2 + '/' + j)
if txt_tym >= log_tym:
print('Issues found in: '+i)
else:
print('No issues found')
I'm using this program to compare timestamp between two files in two different directory, it has same names but different extension,I need to show the result in a text document.If there is an issue it will print Isues found in: filename.I need to print No issues found only if there is no single files with the issue,Im using else inside the loop and it prints multiple times.Please give some suggestions to this
Something like this should work:
issues_found = false
for i in os.listdir(path_1):
for j in os.listdir(path_2):
file_name = (j.split('.')[0])
if i.__contains__(file_name) and i.endswith('txt'):
txt_tym = os.path.getctime(path_1 + '/' + i)
log_tym = os.path.getctime(path_2 + '/' + j)
if txt_tym >= log_tym:
print('Issues found in: '+i)
issues_found = true
if not issues_found:
print('No issues found')

Script to rename and copy files to a new directory.

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).

Resources