Python get meta data for hidden files on windows - python

I have been using the replies from here to read out the metadata of files on windows.
However i noticed that it would just ignore hidden files.
How can one also include hidden files in this approach?

You can combine python's os library with Windows' Shell.Application object, as done here, something like this:
import os
import win32com.client
sh = win32com.client.gencache.EnsureDispatch('Shell.Application', 0)
path = r'c:\mypath\myfolder'
ns = sh.NameSpace(path)
colnum = 0
columns = []
while True:
colname=ns.GetDetailsOf(None, colnum)
if not colname:
break
columns.append(colname)
colnum += 1
for name in os.listdir(path): # list all files
print(path + '\\' + name)
item = ns.ParseName(name)
for colnum in range(len(columns)):
colval=ns.GetDetailsOf(item, colnum)
if colval:
print('\t', columns[colnum], colval)
hidden files will display (H attribute is for Hidden)
...
Attributes HA
...

Related

For Loop Not working as intended - Remove files from folders

Working on a script to automatically remove files from various directories. At first I simply just made an IF statement for each file. This resulted in 3 different statements and while it works, It got me thinking that there has to be a way to use iteration to shorten the code.
Right now everything works right up to the last chunk of code; which is supposed to remove the files from their respective folders.
import os
cwd = os.chdir('C:\\Users\\name\\Desktop\\C008_Start\\')
folder = os.listdir('C:\\Users\\name\\Desktop\\C008_Start')
#Define variables and paths
LMP = 'LMP data for CDRL 008'
WIP = 'WIP Consumption CDRL Financials'
WIPRename = 'WIP Consumption CDRL.xlsx'
JBL = 'JBLM - CDRL S006 CDL2'
xlsx = '.xlsx'
LMPPath = 'C:\\Users\\name\\Desktop\\Python\\lmp\\'
WIPPath = 'C:\\Users\\name\\Desktop\\Python\\wip\\'
JBLPath = 'C:\\Users\\name\\Desktop\\Python\\jbl\\'
#add .xlsx extension to file names
paths = [LMPPath,WIPPath,JBLPath]
filelist = [LMP, WIP, JBL, WIPRename]
filelist2 = []
for item in filelist:
filelist2.append(item + xlsx)
#rename WIP file
if filelist2[1] in folder:
os.rename(filelist2[1], WIPRename)
#remove files from locations
for item in filelist2:
if item in paths:
os.remove(paths + item)
else:
print(item + " not in: " + str(paths))

How to identify Project_Code, File_Name, File_Format. and create new folder structure based of this

I'm not a good python programmer but a good max scripter.
I have been trying to automate a process of cleaning up max files which are corrupted.
There are total of 88000 files which needs to be cleaned up.
The files I get to clean up are in .zip format, with naming conventions like this.
"Project_Name_File_Name_File_Format.zip"
The automation process of loading the .max file and cleaning up the corruption is done via max script.
What i have been trying to do is Create a folder structure like so:
Project Name--> File Name --> File Format
I have been at it for the last two week, still no good progress on this front.
Here is the basic code I have been trying to get it working at least identifying the file names with file formats. I tried a dictionary method for project names and file formats. Still no luck, I created a list to string and then I landed in a loop of creating lists to strings and strings to lists.
import os
files = os.listdir('path\\') # Set location where all the .zip files are present.
files_zip = [i for i in files if i.endswith('.zip')]
for file_name in files_zip:
print(file_name)
token = os.path.splitext(file_name)[0].split("_")
#print(token)
new_token = token[1:-1]
print(new_token)
new_file_name = "_".join(new_token)
print(new_file_name)
Another set of code i tried to do is here
import os
path = 'path\\'
files = os.listdir(path) # Set location where all the .zip files are present.
# Dictionary Project Keys and Values
project_dic = {'ABC': 'Apple Bucket Cake', 'XYZ': 'Xerox Yacht Zoo'}
# Dictionary for File Formats
file_formats = {'FBX': 'FBX', 'OBJ': 'OBJ', '3ds Max': '3ds Max'}
# Looking for the files which ends with Project Names (prj_lst)
files_txt = [i for i in files if i.endswith('.zip')]
# print(files_txt)
prj_lstToStr = ' '.join([str(elem) for elem in files_txt])
name_set = prj_lstToStr.split('prj_lstToStr')
# print(name_set)
#print("Project_List : " + str(name_set))
res = [ele if len(ele) > 0 else () for ele in [[key for key in project_dic if key in sub] for sub in name_set]]
#print("Project_Matching_Keys : " + str(res))
string_key = ''.join(str(res))
format_list = [ele if len(ele) > 0 else () for ele in [[key for key in file_formats if key in sub] for sub in name_set]]
#print("Format_Matching_Keys : " + str(format_list))
format_key = ''.join(str(format_list))
token = files_txt
I don't know what exactly you're trying to do, so I will delete this answer if it is not correct. From what I understand:
input = ["BC_Warren_Vidic_Head_OBJ.zip",
"ALS_Sand_Mound_pr_ann_0479_c_OBJ.zip",
"GRT_ENV-SPE-GRP-SK-ExplorationChestPMC-A_3dsMax.zip",
"KLS_alpha-GEN_PRO_HedgePotPlanter_Group_01A_2021-03-31_FBX.zip",
"MISC_gho_caucasian-mattE_(wise)_OBJ.zip",
"MISC_W_ATT_SalvoXL_FBX.zip",
"MISC_XA-20_Razorback_JetFighter_3dsMax.zip",
"WLD_ENV-GLO-PRO-Bivouac-TacticalSmartphone-A_3dsMax.zip",
"XYZ_WPN_ATT_MAG_MagpulPMAGMOE_FBX.zip"]
for inp in input:
splitted = inp.split('_')
project_name = splitted[0]
file_name = '_'.join(splitted[1:-1])
file_format = splitted[-1].split('.')[0]
path = f'./{project_name}/{file_name}/{file_format}'
os.makedirs(os.path.dirname(path), exist_ok=True)

How can I clasify files based on their extension in Python?

I want to move files into folders based on their extensions and categorize them.
I've tried shutil.move() to categorize it. But it gives an error like this:
shutil.Error: Cannot move a directory 'C:\Users\user\Desktop\deneme' into itself 'None'.
How can i fix the problem?
My code:
import os
from os import path
import pathlib
import shutil
path_ = input("Directory: ")
file_list = os.listdir(path_)
os.chdir(path_)
current_directory = os.getcwd()
e = []
a = 0
for i in file_list:
ext = os.path.splitext(i)[1][1:]
e.append(ext)
# print("Ext:", ext)
for j in range(len(e)):
if e[j] == ",":
j = j + 1
continue
os.mkdir(str(j))
os.rename(str(j), e[j])
new_folder = e[j]
for f in os.listdir(current_directory):
new_directory = os.chdir(new_folder)
if f == ",":
f +=1
continue
shutil.move(os.path.join(current_directory), str(new_directory))
#print("it is moved")
print(os.path.dirname(os.path.abspath(str(e[j]))))
Try os.path.splitext()[1] (it returns a list. The 0th element is the filename and the 1st is the extension) if you want to find the file extension

How to control Os iterations python

I wrote the following code to make an inventory of every files in a library. the idea is that the 3 columns have infromation about the file.
1) complete path 2) name of the parent directory 3) filename.
import os
import openpyxl
def crearlista (*arg, **kw):
inventario = openpyxl.Workbook(encoding = "Utf-8")
sheet = inventario.active
i = 1
f = 1
e = ""
for dirpath, subdirs, files in os.walk(*arg, **kw):
for name in subdirs:
e = os.path.join (name)
for name in files:
sheet.cell(row=i, column=3).value = name
sheet.cell(row=i, column=1).value = dirpath
sheet.cell(row=i, column=2).value = e
i = i + 1
inventario.save("asd3.xlsx")
crearlista("//media//rayeus/Datos/Mis Documentos/Nueva carpeta/", topdown=False)
The problem is that it iterates first through the files in the first folder and after that starts filling the 'e' variable with the name of the first folder.
That way it starts writing late the names in the folder column. And it writes theme as many times as files in the next folder, not as many files there are in THAT folder.
How can i solve this?

Multiple Paths Traversed and Displayed Filed type in Maya Menu with Python

I'm new here so bare in mind that and I hope my questions are clearly asked for you lot to help me out. I am trying to alter Brent Tylers Dropbox script so that I will be able to list Python under Python, Mel under Mel and so on(eventually plugins and other files too but not for now)
Ok so my directory is like so:
1.
sf=C:/users/scripts/ a.py + b.mel
pf=C:/users/scripts/Python/c.py
mf=C:/users/scripts/Mel/d.mel
(These are the folders my scripts will be placed in)
Code :
absoluteFiles = []
relativeFiles = []
folders = []
allFiles = []
currentFile = ''
for root, dirs, files in os.walk(sf):
for x in files:
correct = root.replace('\\', '/')
currentFile = (correct + '/' + x)
allFiles.append(currentFile)
if currentFile.endswith('.mel'):
relativeFiles.append(currentFile.replace((mf + '/'), ""))
if currentFile.endswith('.py'):
relativeFiles.append(currentFile.replace((pf + '/'), ""))
relativeFiles.sort()
for relativeFile in relativeFiles:
split = relativeFile.split('/')
fileName = split[-1].split('.')
i=0
while i<(len(split)):
### Create Folders ###
if i==0 and len(split) != 1:
if cmds.menu(split[i] ,ex=1) == 0:
cmds.menuItem(split[i], p=PadraigsTools, bld=1, sm=1, to=1, l=split[i])
if i > 0 and i < (len(split)-1):
if cmds.menu(split[i] ,ex=1) == 0:
cmds.menuItem(split[i], p=split[i-1], bld=1, sm=1, to=1, l=split[i])
### Create .mel Files ###
if fileName[-1] == 'mel':
if i==len(split)-1 and len(split) > 1:
scriptName = split[-1].split('.')
temp1 = 'source ' + '"' + sf + '/' + relativeFile + '"; ' + scriptName[0]
command = '''mel.eval(''' + "'" + temp1 + '''')'''
cmds.menuItem(split[i], p=split[i-1], c=command, l=split[i])
if i==len(split)-1 and len(split) == 1:
scriptName = split[-1].split('.')
temp1 = 'source ' + '"' + sf + '/' + relativeFile + '"; ' + scriptName[0]
command = '''mel.eval(''' + "'" + temp1 + '''')'''
cmds.menuItem(split[i], p=Mel, c=command, l=split[i])
### Create .py Files ###
if fileName[-1] == 'py':
if i==len(split)-1 and len(split) > 1:
command = 'import ' + fileName[0] + '\n' + fileName[0] + '.' + fileName[0]+ '()'
cmds.menuItem(split[i], p=split[i-1], c=command, l=split[i])
if i==len(split)-1 and len(split) == 1:
command = 'import ' + fileName[0] + '\n' + fileName[0] + '.' + fileName[0]+ '()'
cmds.menuItem(split[i], p=Python, c=command, l=split[i])
i+=1
So far I can print out individually (sf, pf, mf) to the corresponding Directory but I cant list out everything at once and the files under sf will not show at all. regarding the folders created it ends up very odd. sometimes i would get a duplicate folder as a submenu and if i use sf it give me C:/.
After days and hours of research trying to mend this script I have found no answer including
from itertools import chain
paths = (mf, sf, pf)
for path, dirs, files in chain.from_iterable(os.walk(path) for path in paths):
::QUESTION::
Is there a way i can put this together sanely so that new folders will show up with their contents on refresh as a submenu and the files will show up and allow me to execute them from their corresponding submenu.
I would appreciate any help possible including down votes haha. And bare in mind I don't want you to hand me the answer on a golden spoon because I wont know what is corrected or needs to be :)
Thanks Greatly
-- Padraig
There's a couple of things you can do to simplify things a bit.
First, it's a good idea to make this as data-driven as possible so you don't have to re-write it if your needs change. This does more or less what you do, but collects the results into a dictionary where the key are the root paths you supplied and the values are lists of relative paths:
def find_files(root, extensions = ('mel', 'py')):
def clean_path(*p):
return "/".join(p).replace('\\', '/')
for root, _, files in os.walk(root):
used = [f for f in files if f.split(".")[-1] in extensions]
for u in used:
yield clean_path(root, u)
def relativize(abs, roots):
low_roots = map (str.lower, roots) # all lower for comparison
for root, low_root in zip(roots,low_roots):
if abs.lower().startswith(low_root):
return root, abs[len(root):]
return ("", abs)
relative_paths = find_files('c:/users/scripts')
root_dict = {}
for item in relative_paths :
folder, file = relativize(item, ('C:/users/scripts/Python/', 'C:/users/scripts/Mel/', 'C:/users/scripts/'))
if not folder in root_dict:
root_dict[folder] = []
root_dict[folder].append(file)
So now you have a dictionary with a bunch of root folders and lists of relative paths (files that were not in any relative path you supplied are keyed to empty string and show up as absolute paths). You can make the menus in a very generic way because they are all in the same format. If you need the entire list, you can get it like this:
results = []
for each_root in root_dict:
for relpath in root_dict[each_root]:
results.append(each_root + relpath)
For creating the actual menus, you want to use a single function and bind it to the filename for each menu item as you make it. This is a slightly tricky topic (more detail here). The easy way to do this is to use a functools.partial object, which bundles a command and a bunch of arguments into an object which looks like a function: you can create a partial and attach it to the command of your menu items so they all call the same function with their individual arguments. Here's a simple example using the variables from above and a menubar; you can see how to adapt it to other kinds of menus pretty easily:
from functools import partial
# call this on every button selection
def test(filepath, ignore):
# maya will send "test(name, False)"; we just ignore the 'False'
print "Here's where I would reload", filepath
example = cmds.window(title = 'example')
menubar = cmds.menuBarLayout()
for name in folder_names:
menuname = name
if menuname:
menuname = menuname.split("/")[-2] # we used trailing slashes
else:
menuname = "root"
cmds.menu(label = menuname)
file_names = root_dict[name]
file_names.sort()
for fn in file_names:
mi = cmds.menuItem(label = fn, command = partial(test, fn))
cmds.setParent(menubar)

Categories

Resources