Spaces:
Runtime error
Runtime error
import os | |
from os import path | |
def rename_file(old_path, new_path): | |
""" | |
νμΌ μ΄λ¦μ μμ νλ ν¨μ | |
Parameters: | |
old_path (str): νμΌ κ²½λ‘ + κΈ°μ‘΄ νμΌ μ΄λ¦ | |
new_path (str): νμΌ κ²½λ‘ + μμ ν νμΌ μ΄λ¦ | |
Changes: | |
old_path > new_path | |
""" | |
if path.exists(new_path)==False: | |
if path.exists(old_path): | |
# νμΌμ΄ μλ λλ ν 리 κ²½λ‘ μ»κΈ° | |
src = path.realpath(old_path) | |
# μλ³ΈνμΌ νμΌλͺ λ³κ²½ | |
os.rename(old_path,new_path) | |
else: | |
print("{old_path} doesn't exist") | |
def extract_text_from_filename(filename): | |
""" | |
νμΌ μ΄λ¦μμ 'x'μ 'y' μ¬μ΄μ ν μ€νΈμ 'y'μ 'w' μ¬μ΄μ ν μ€νΈλ₯Ό μΆμΆνλ ν¨μ | |
ex) x10y148w157h184.jpg | |
Parameters: | |
filename (str): νμΌ μ΄λ¦ | |
Returns: | |
tuple: 'x'μ 'y' μ¬μ΄μ ν μ€νΈ, 'y'μ 'w' μ¬μ΄μ ν μ€νΈ | |
""" | |
start_x = filename.index('x') | |
end_x = filename.index('y') | |
start_y = end_x + 1 | |
end_y = filename.index('w') | |
x_text = filename[start_x+1:end_x] | |
y_text = filename[start_y:end_y] | |
return x_text, y_text | |
def get_folder_contents_with_text(folder_path, file_extension=".jpg"): | |
""" | |
ν΄λ λ΄λΆμ νμΌλ€μ μ΄λ¦μμ 'x'μ 'y' μ¬μ΄μ ν μ€νΈλ₯Ό 리μ€νΈ xμ, | |
'y'μ 'w' μ¬μ΄μ ν μ€νΈλ₯Ό 리μ€νΈ yμ κ°κ° μ μ₯νλ ν¨μ | |
Parameters: | |
folder_path (str): ν΄λ κ²½λ‘ | |
file_extension (str, optional): κ°μ Έμ¬ νμΌμ νμ₯μ (κΈ°λ³Έκ°: ".jpg") | |
Returns: | |
list, list: 'x'μ 'y' μ¬μ΄μ ν μ€νΈλ₯Ό μ μ₯ν 리μ€νΈ x, | |
'y'μ 'w' μ¬μ΄μ ν μ€νΈλ₯Ό μ μ₯ν 리μ€νΈ y | |
""" | |
x_list = [] | |
y_list = [] | |
folder_contents = os.listdir(folder_path) | |
print(folder_contents) | |
jpg_files = [filename for filename in folder_contents if filename.endswith('jpg')] | |
for jpg_file in jpg_files: | |
x_text, y_text = extract_text_from_filename(jpg_file) | |
x_list.append(x_text) | |
y_list.append(y_text) | |
return x_list, y_list, jpg_files |