Today I Learned

ํŒŒ์ด์ฌ ํด๋” ์ •๋ฆฌ ์ž๋™ํ™”ํ•˜๊ธฐ (os.mkdir/os.path.join)

by Holly Yoon

๐Ÿ’๐Ÿป‍โ™€๏ธ ํด๋” ๋‚ด์— ๋‹ค์–‘ํ•œ ํŒŒ์ผ๋“ค์„ ์ •๋ฆฌํ•ด๋ณด์ž!

1)ํŠน์ • ์ œ๋ชฉ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŒŒ์ผ ์ •๋ฆฌํ•˜๊ธฐ - Screen Shot xxx
2)ํŠน์ • ํ™•์žฅ์ž๋ช…์„ ๊ฐ€์ง„ ํŒŒ์ผ ์ •๋ฆฌํ•˜๊ธฐ - ์ด๋ฏธ์ง€ ํŒŒ์ผ (png, jpg, jpeg๋“ฑ)

OS ๋ชจ๋“ˆ 

 ํŒŒ์ด์ฌ์— ๊ธฐ๋ณธ ๋‚ด์žฅ๋˜์–ด์žˆ๋Š” ๋ชจ๋“ˆ๋กœ os์€ ์šด์˜์ฒด์ œ ์ข…์† ๊ธฐ๋Šฅ์— ๋Œ€ํ•œ ๊ฐ„๋‹จํ•œ ๋ช…๋ น์„ ๋ชจ์•„๋†“์€ ๊ธฐ๋ณธ๋ชจ๋“ˆ์ด๋‹ค. ์ž์„ธํ•œ ๋ฌธ์„œ๋Š” python docs

ํด๋” ์ƒ์„ฑ - os.mkdir(๊ฒฝ๋กœ)

import os

#์ƒ๋Œ€ ๊ฒฝ๋กœ
os.mkdir('file')
os.mkdir('folder/file')

#์ ˆ๋Œ€ ๊ฒฝ๋กœ
os.mkdir('user/documents/folder')
  • os.mkdir์€ ํŒŒ์ด์ฌ์—์„œ ํด๋”๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜
  • ์ง€์ •ํ•œ ๊ฒฝ๋กœ์— ๊ฐ™์€ ์ด๋ฆ„์˜ ํด๋”๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
#์˜ค๋ฅ˜์—†์ด ์‹คํ–‰๋˜๊ธฐ
path = '/Users/hollyyoon/Documents/folder'
os.makedirs(path, exist_ok=True)

#ํด๋”๊ฐ€ ์—†์„๋•Œ๋งŒ ๋งŒ๋“ค๊ธฐ
if not os.path.exists('/Users/hollyyoon/Documents/folder'):
	mkdirs('/Users/hollyyoon/Documents/folder')
  • ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ makedirs๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  exist_ok=True ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ํฌ๋”๊ฐ€ ์กด์žฌํ•˜๋”๋ผ๋„ ์˜ค๋ฅ˜์—†์ด ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • ํ˜น์€ ์กฐ๊ฑด์‹์„ ํ™œ์šฉํ•˜์—ฌ, ๊ฐ™์€ ์ด๋ฆ„์˜ ํด๋”๊ฐ€ ์—†์„ ๋•Œ๋งŒ ๋งŒ๋“ค๋„๋ก ๋ช…๋ นํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํŒŒ์ผ ๋ฆฌ์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ - os.listdir(๊ฒฝ๋กœ)

import os
file_list = os.listdir("/Users/hollyyoon/Desktop")
  • ๊ฒฐ๊ณผ๋Š” ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

ํŒŒ์ผ ๊ฒฝ๋กœ - os.path()

1) os.path.split / splitext

#path๋ฅผ ๋””๋ ‰ํ† ๋ฆฌ์™€ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌ
import os
os.path.split("/Users/hollyyoon/Desktop/images.png")

#์‹คํ–‰ ๊ฒฐ๊ณผ "/Users/hollyyoon/Desktop", "images.png"

#ํŒŒ์ผ ์ด๋ฆ„๊ณผ ํ™•์žฅ์ž๋ช… ๋ถ„๋ฆฌ
import os
os.path.splitext("/Users/hollyyoon/Desktop/images.png")

#์‹คํ–‰ ๊ฒฐ๊ณผ "images", ".png"
  • splitextํ•จ์ˆ˜๋Š” ํŠœํ”Œ(๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์ž๋ฃŒํ˜•) ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

2) os.path.join

#๊ฐ๊ฐ์˜ ๊ฒฝ๋กœ๋ฅผ ํ•˜๋‚˜๋กœ ์ด์–ด์คŒ
import os
os.path.join("/Users/hollyyoon","Desktop","images.png")

#์‹คํ–‰ ๊ฒฐ๊ณผ "/Users/hollyyoon/Desktop/images.png"

 

ํŒŒ์ผ ์ •๋ฆฌ ์ž๋™ํ™”ํ•˜๊ธฐ

  1. ํด๋” ์ง€์ •ํ•˜๊ธฐ (์ •๋ฆฌํ•  ํด๋”/์ •๋ฆฌํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ƒˆ ํด๋”)
  2. ์ •๋ฆฌํ•  ํด๋”์— ์žˆ๋Š” ํŒŒ์ผ ํ™•์ธํ•˜๊ธฐ (ํ™•์žฅ์ž๋ช… ํ™•์ธ)
  3. ํƒ€๊ฒŸ ํŒŒ์ผ๋“ค์„ ์ •๋ฆฌํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐˆ ํด๋”๋กœ ๊ฒฝ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ธฐ

1)ํŠน์ • ์ œ๋ชฉ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ํŒŒ์ผ ์ •๋ฆฌํ•˜๊ธฐ - Screen xxx

import os

before = '/Users/hollyyoon/Desktop'
after = '/Users/hollyyoon/Desktop/screenshot'
if not os.path.exists('/Users/hollyyoon/Desktop/screenshot'):
	os.mkdir('/Users/hollyyoon/Desktop/screenshot')

file_list = os.listdir(before)
for file in file_list :
    if file.startswith('Screen'):
        before_file = os.path.join(before,file)
        after_file = os.path.join(after,file)
        os.rename(before_file, after_file)

 

2)ํŠน์ • ํ™•์žฅ์ž๋ช…์„ ๊ฐ€์ง„ ํŒŒ์ผ ์ •๋ฆฌํ•˜๊ธฐ - ์ด๋ฏธ์ง€ ํŒŒ์ผ (png, jpg, jpeg๋“ฑ)

import os

before = '/Users/hollyyoon/Downloads'
after = '/Users/hollyyoon/Downloads/img'
ext_list = ['.jpg', '.png', '.jpeg', '.gif', ',HEIC']
if not os.path.exists('/Users/hollyyoon/Downloads/img'):
	os.mkdir('/Users/hollyyoon/Downloads/img')

file_list = os.listdir(before)
for file in file_list :
    name, ext = os.path.splitext(file)
    if ext in ext_list:
        before_file = os.path.join(before,file)
        after_file = os.path.join(after,file)
        os.rename(before_file, after_file)

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Log by Holly

Holly Yoon

ํ™œ๋™ํ•˜๊ธฐ