Placing HTML file and associated folder into same directory -- Python on macOS Monterey
by ericlindellnyc from LinuxQuestions.org on (#6PC1Q)
I have the following code to take an HTML file and its associated folder (usually ending, i_filesi) and place them into a new folder containing both.
The print statement in the first for loop shows that filename is being properly assigned the file name. Except that the i.htmli is not being stripped, as the code attempts to do. Analogously for the folder name.
So the script compares the names of file and folder, and itis not a match -- which it must be for the script to work.
How can I make the names of folder and file match -- after removing the suffixes?
Suggestions appreciated !!
Code:files = {}
folders = {}
for entry in scandir('/Users/ericlindell/testHtmlDirInSameFolder/HnDirs'):
if entry.is_file() and entry.name.endswith('.html'):
# Collect the file.
files[entry.name.removesuffix('.HTML')] = entry.name
filename = files[entry.name.removesuffix('.HTML')]
print('filename is ', filename)
elif entry.is_dir() and entry.name.endswith('_files'):
# Collect the folder.
folders[entry.name.removesuffix('_files')] = entry.name
# Which names occur in both the dict of files and the dict of folders?
common = files.keys() & folders.keys()
for name in common:
print(f'Found a file called {files[name]} and a folder called {folders[name]}')
parent_folder = '/Users/ericlindell/testHtmlDirInSameFolder/'
for name in common: # Make subfolder for HTML file & folder, & move them in.
mkdir(join(parent_folder, name))
rename(files[name], join(parent_folder, basename(files[name])))
rename(folders[name], join(parent_folder, basename(folders[name])))
The print statement in the first for loop shows that filename is being properly assigned the file name. Except that the i.htmli is not being stripped, as the code attempts to do. Analogously for the folder name.
So the script compares the names of file and folder, and itis not a match -- which it must be for the script to work.
How can I make the names of folder and file match -- after removing the suffixes?
Suggestions appreciated !!
Code:files = {}
folders = {}
for entry in scandir('/Users/ericlindell/testHtmlDirInSameFolder/HnDirs'):
if entry.is_file() and entry.name.endswith('.html'):
# Collect the file.
files[entry.name.removesuffix('.HTML')] = entry.name
filename = files[entry.name.removesuffix('.HTML')]
print('filename is ', filename)
elif entry.is_dir() and entry.name.endswith('_files'):
# Collect the folder.
folders[entry.name.removesuffix('_files')] = entry.name
# Which names occur in both the dict of files and the dict of folders?
common = files.keys() & folders.keys()
for name in common:
print(f'Found a file called {files[name]} and a folder called {folders[name]}')
parent_folder = '/Users/ericlindell/testHtmlDirInSameFolder/'
for name in common: # Make subfolder for HTML file & folder, & move them in.
mkdir(join(parent_folder, name))
rename(files[name], join(parent_folder, basename(files[name])))
rename(folders[name], join(parent_folder, basename(folders[name])))