๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/TIL(Today I Learned)

TIL_220703_ํŒ€ ํ”„๋กœ์ ํŠธ

  • -

์œ ํ™” ์ œ์ž‘ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋กœ์ง์— ๋Œ€ํ•ด์„œ

๋”ฅ๋Ÿฌ๋‹ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์“ฐ์˜€๋˜ ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ ์กฐ์‚ฌํ•ด๋ณด์•˜๋‹ค.


default_storage

default_storage๋Š” Django์—์„œ file manage๋ฅผ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

FileSystemStorage ํด๋ž˜์Šค๋Š” ๋กœ์ปฌ ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ๊ธฐ๋ณธ ํŒŒ์ผ ์Šคํ† ๋ฆฌ์ง€๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. ๋ณ„๋„์˜ ์ธ์ž๊ฐ’ ์—†์ด ์ƒ์„ฑํ•œ๋‹ค๋ฉด

ํŒŒ์ผ์„ ์ €์žฅํ•  ๋””๋ ‰ํ† ๋ฆฌ์˜ Root ๊ฒฝ๋กœ๋Š” settings.py์˜ MEDIA_ROOT ๊ฐ’์ด๋ฉฐ, ์ €์žฅ๋œ ํŒŒ์ผ์„ servingํ•˜๋Š” URL ๋˜ํ•œ

settings.py์˜ MEDIA_URL ๊ฐ’์ด๋‹ค.

 

default_storage์˜ ์ฃผ์š” ๋ฉ”์†Œ๋“œ

from django.core.files.storage import default_storage

file = request.FILES['file']

# ์ง€์ •๋œ ๊ฒฝ๋กœ์— ํŒŒ์ผ ์ €์žฅ
path = default_storage.save('path/to/file', file)
print(path)        # 'path/to/file'

# ํŒŒ์ผ ์ „์ฒด ํฌ๊ธฐ(byte)
default_storage.size(path)        # 11
# ์ง€์ •๋œ ํŒŒ์ผ ์—ด๊ธฐ
default_storage.open(path).read() 

# ์ง€์ •๋œ ํŒŒ์ผ ์‚ญ์ œ
default_storage.delete(path)
# ์ง€์ •๋œ ํŒŒ์ผ ์กด์žฌ ์—ฌ๋ถ€
default_storage.exists(path)        # False

 

[ํ”„๋กœ์ ํŠธ ์ ์šฉ]

# ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ๋ฅผ ์œ„ํ•œ ์ž„์‹œ ํด๋” ์ƒ์„ฑ : style_transfer/input
default_storage.save('style_transfer/input/input_img.jpg', ContentFile(file.read()))

 

[์ถœ์ฒ˜] : https://chagokx2.tistory.com/62

 

[Django Basic 13] default_storage

default_storage default_storage ๋Š” Django์—์„œ file manage๋ฅผ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‹ค. Django์˜ ๊ธฐ๋ณธ ํŒŒ์ผ ์ €์žฅ์†Œ๋Š” settings.py ์˜ DEFAULT_FILE_STORAGE ์„ค์ •์— ์˜ํ•ด ์ œ๊ณต๋œ๋‹ค. ํ•ด๋‹น ์„ค์ • ํ•ญ๋ชฉ์˜ defau..

chagokx2.tistory.com

 


 

shutil.rmtree()

ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ ์‚ญ์ œํ•˜๊ธฐ

shutil ๋ชจ๋“ˆ์—๋Š” ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์ด ํฌํ•จ๋ผ ์žˆ์ง€ ์•Š์€ ๋“ฏํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜์ธ rmtree()๋Š” ๋ถ„๋ช… ํฌํ•จ๋ผ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ญ์ œ๋Š” ๋˜๋Œ๋ฆด ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์‚ญ์ œ๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ํœด์ง€ํ†ต์—์„œ ์ฐพ์„ ์ˆ˜ ์—†์„ ํ…Œ๋‹ˆ ์ฃผ์˜ํ•˜์„ธ์š”.

๋‹ค์Œ ์˜ˆ์ œ๋Š” Original-Copy ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์˜๊ตฌ์ ์œผ๋กœ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

import shutil
shutil.rmtree('Original-Copy')

 

[ํ”„๋กœ์ ํŠธ ์ ์šฉ]

shutil.rmtree('style_transfer/input/')

 

[์ถœ์ฒ˜] : https://code.tutsplus.com/ko/tutorials/file-and-directory-operations-using-python--cms-25817

 

ํŒŒ์ด์ฌ์„ ์ด์šฉํ•œ ํŒŒ์ผ ๋ฐ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ž‘์—…

์ด์ „ ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ํŒŒ์ผ์„ ์ฝ๊ณ , ์—ด๊ณ , ๋‹ซ๊ณ , ์“ฐ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ(ํด๋”)์—์„œ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์ž‘์—…์— ๋Œ€ํ•ด ์ข€ ๋” ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ฌ

code.tutsplus.com

 


glob ๋ชจ๋“ˆ์˜ glob ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฒ•

ํŠน์ •ํ•œ ํŒจํ„ด์ด๋‚˜ ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ ํŒŒ์ผ๋“ค์˜ ๊ฒฝ๋กœ๋‚˜ ์ด๋ฆ„์ด ํ•„์š”ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.

glob ๋ชจ๋“ˆ์˜ glob ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ œ์‹œํ•œ ์กฐ๊ฑด์— ๋งž๋Š” ํŒŒ์ผ๋ช…์„ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋‹จ, ์กฐ๊ฑด์— ์ •๊ทœ์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์—‘์…€ ๋“ฑ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” '*'์™€ '?'๊ฐ™์€ ์™€์ผ๋“œ์นด๋“œ๋งŒ์„ ์ง€์›ํ•œ๋‹ค.

 

[ํ”„๋กœ์ ํŠธ ์ ์šฉ]

list_of_files = glob.glob('style_transfer/output/*')  # * means all if need specific format then *.csv

 

[์ถœ์ฒ˜] : https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=siniphia&logNo=221397012627 

 

[Python] glob.glob() ์‚ฌ์šฉ๋ฒ•

1. ๊ฐœ์š” ๋งŽ์€ ํŒŒ์ผ๋“ค์„ ๋‹ค๋ค„์•ผ ํ•˜๋Š” ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•  ๋•Œ, ํŠน์ •ํ•œ ํŒจํ„ด์ด๋‚˜ ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ ํŒŒ์ผ๋“ค...

blog.naver.com

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.