Python Forum
How do I add comments from a text-file to an array of folders?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I add comments from a text-file to an array of folders?
#1
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)
Reply
#2
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?
Reply
#3
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 542 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,136 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  extract only text strip byte array Pir8Radio 7 3,000 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,193 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,702 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,018 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,953 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,326 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,377 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,871 Dec-06-2020, 04:14 PM
Last Post: Divya577

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020