I have to create an app that gets a path from the user and renames, after the content of a cell, the entire document (excel documents in this case). For example, if in a specified cells, there is “2”, the document will be names 2.xlsx and so on, until al the files are renamed properly.
The problem is that, after verifying that, in the specified path, there are files, python exits with [Errno2] no such file or directory. I’ve modified the code to add for files in os.listdir(pathlib.Path(SPEC_PATH).resolve()):
, that apparently solves the issue with the error, but doesn’t recognize some components (traceback below).
I tried debugging it, but, to me it seems fine. What might be the problem? Are there some bugs that i need to take care of first?
The code:
import os
import pandas as pd
import pathlib
import re
from openpyxl import Workbook, load_workbook
# global variables
SPEC_PATH = input("path: ")
def format_date(extracted_string):
new_s = re.sub(r"[^a-zA-Z0-9 ]", "", extracted_string)
return new_s
def extract_date(workbook_name):
for files in os.listdir(pathlib.Path(SPEC_PATH).resolve()):
if files.endswith(".xlsx") or files.endswith(".xls"):
new_path = os.path.join(SPEC_PATH, files)
wb = load_workbook(new_path)
ws = wb.active
return ws['C5'].value
# main function
SPEC_PATH = pathlib.Path(SPEC_PATH).resolve() # will access the given path
count = 0
user_choice = input(
"Would you like the new name to contain a string before the date?(y/n) ").lower()
while True:
if user_choice.isalpha():
if user_choice == 'y':
head_string = input("Please enter the string: ")
for x in os.listdir(SPEC_PATH):
# might include all xl extensions just to be safe
if x.endswith(".xls") or x.endswith(".xlsx"):
extract_date(x)
format_date(x)
for file in os.path.join(SPEC_PATH, file):
if file.is_file():
custom_name = extract_date(file)
new_name = f"{head_string}{file.suffix}"
if SPEC_PATH.is_file():
continue
else:
try:
file.rename(SPEC_PATH/new_name)
print("Success!")
except OSError as e:
print(e)
else:
if user_choice == 'n':
# will count all the files that contain a specified extension
for files in os.listdir(SPEC_PATH):
# might include all xl extensions just to be safe
if files.endswith(".xls") or files.endswith(".xlsx"):
count += 1
extract_date(files)
format_date(files)
if files.is_file():
custom_name = extract_date(files)
new_name = f"{files.suffix}"
if SPEC_PATH.is_file():
continue
else:
try:
file.rename(SPEC_PATH/new_name)
print("Success!")
except OSError as e:
print(e)
else:
if user_choice.isnumeric() or user_choice != 'y' or user_choice != 'n':
print("Please enter a valid choice!")
break
Also, i have this error now:
File "e:\renameFiles\smallComponentsTest.py", line 62, in <module>
if files.is_file():
AttributeError: 'str' object has no attribute 'is_file' ```
Advertisement
Answer
This fixed all the problems and finished the renaming sequence.
import os
import pathlib
if user_choice == 'y':
head_string = input("Please enter the string: ")
# renaming operation
for x in os.listdir(SPEC_PATH):
full_path = os.path.join(SPEC_PATH, x)
extension = pathlib.Path(full_path).suffix
custom_name = extract_date(full_path)
new_name = f"{head_string}{custom_name}{extension}"
try:
os.rename(os.path.join(SPEC_PATH, x),
os.path.join(SPEC_PATH, new_name))
print("Success!")
counter += 1
except OSError as e:
print(e)
counter += 1