Python Forum

Full Version: How do I add comments from a text-file to an array of folders?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone -

I was just wondering, as I’ve ventured out on bit of a limp here, if anyone have any experience with automating the process of creating folders in macOSX, and in the same process, adding individual comments to each of the folders?

I’ve managed to write the code that creates each of the folders, based on an external txt-file. Now, I want another external txt-file to be the basis of each comment/description that I want to add to each of the created folders.

I feel like I’ve tried everything. Maybe I am getting at this the wrong way … any help or suggestions are very much welcome!!

Thanks in advance.

Claus

import os
import xattr
from osxmetadata import *

folderstextfile = '/Users/clausneergaard/Dropbox/Projekter/21-01 Revit families, dokumentation/Folders (empty)/FoldersList.txt'
rootfolder = '/Users/clausneergaard/Dropbox/Projekter/21-01 Revit families, dokumentation/Folders (empty)/Folders'
metadata = '/Users/clausneergaard/Dropbox/Projekter/21-01 Revit families, dokumentation/Folders (empty)/FoldersMeta.txt'

# Open the file containing the list of names for each folder
with open(folderstextfile, 'r') as f:
  
  # Read the names from the file, one name per line
  names = f.read().splitlines()

# Open the file containing the list of descriptions for each folder
with open(metadata, 'r') as m:
  
  # Read the meta-data from the file, one piece of data per line
  meta = m.read().splitlines()

# Create a folder for each name in the list, and also add a comment to each folder in the list
for name in names:
  
  folders = os.makedirs(os.path.join(rootfolder, name))
  osxmetadata.MDItemSetAttribute("This is a comment", "Description")
  comments = xattr.setxattr('Folders', 'Comments', meta)
What happens when you run the code showed? Does it work? Do you get an error?

What's in the metadata file? Is that the file holding the comments? Are they also one per line?
use mkdir, set exist_ok to True so existing directories won't be overwritten.

example:
from pathlib import Path
import os

#set current directory same as python script (or to your target directory):
os.chdir(os.path.abspath(os.path.dirname(__file__)))

homepath = Path('.')
newdir = homepath / f"Mynewdir"
newdir.mkdir(exist_ok=True)
[/python]