파이썬으로 스크립트를 짜보고 있는 초보인데요,
nvt_00 부터 nvt_90 까지의 폴더들 안에 포함된, pr_nvt_00.mdp ~ 90번까지의 파일들을
반복문 for를 이용해서 수정하려고, 아래와 같이 파일을 읽게 하려고 했는데
open_file = open("nvt_%02d/pr_nvt_%02d.mdp" % (temp_set.index(temp_option), temp_set.index(temp_option)), "r")
빨간색 부분을 추가 하니까 안되네요ㅠ. 어떤 방법이 있는지 여쭈어봅니다.
½±°Ô ¹Ýº¹¹®À¸·Î µ¹·Á¼ Çϼ¼¿ä...
open_file = open("pr_nvt.mdp", "r") ÀÌ·±½ÄÀ¸·Î ÆÄÀÏ À̸§À» ÁöÁ¤ÇÏ¸é µÇ´Âµ¥... ¿©·¯°³¸¦ ó¸®ÇØ º¸·Á°í À§¿¡ ó·³ ÇÏ´Ï±î ¾ÈµÇ³×¿ä ¤Ð
http://stackoverflow.com/questions/3484348/dynamic-filenames
ÂüÁ¶Çغ¸¼¼¿ä ´Ù¾çÇÑ ¹æ¹ýÀÌ Àֳ׿ä... Àú·± ¹æ¹ýÀÌ ½¬¿ì½Ã¸é ¸Ç À§¿¡..
for x in range(22): #Remember that the range function returns integers up to 22-1
exec "f%02d = open('file%02d.txt', 'w')" % (x, x)
¿ä°í ¾²½Ã¸é µÉ°Í °°³×¿ä .. ¾ÈÂÊÀÌ ¾Æ´Ñ ¹Ù±ùÂÊ¿¡´Ù ³Ö´Â°Å³×¿ä..
Ãʺ¸¶ó¼ ¸çÄ¥µ¿¾È °Ü¿ì°Ü¿ì Á¶±Ý¾¿ ÇÏ°í ÀÖ½À´Ï´Ù ¤¾¤¾
ÀÏ´Ü ÆÄÀ̽ãÀ¸·Î ÆÄÀÏÀ» ÀÐÀ¸·Á¸é os ¸ðµâ »ç¿ë¹ý °£·«ÇÏ°Ô ¾Ë¾ÆµÎ¼Å¾ß ÇÕ´Ï´Ù. ¸¹À̵µ ÇÊ¿ä¾ø°í ÀÚÁÖ¾²´Â°Í¸¸...
À̹ø ±Û¿¡¼´Â ¹®ÀÚ¿ Æ÷¸ÅÆÃÀ» À߸ø ÇϽŠ°Í °°½À´Ï´Ù. Æ÷¸ÅÆÃÀÌ ¹«¾ùÀÎÁö´Â ¾Æ½ÃÁÒ?
Áö±Ý ¹®¸ÆÀÌ ÀÌÇØ°¡ ¾È°¡´Âµ¥ ´õ ÀÚ¼¼È÷ Àû¾îÁÖ½Ç ¼ö ÀÖ³ª¿ä?
Àú±â¿¡ temp_set.index³ª temp_optionÀÌ À§¿¡¼ defÇϽŰǰ¡...
Á¦°¡ ÀÛ¼ºÇØ ³õÀºµ¥ ±îÁö ÀÔ´Ï´Ù.
import os
import shutil
temp_set = (300.00, 301.66, 303.33, 305.01, 306.69, 308.38, 310.08, 311.79, 313.51, 315.24, 316.97,
318.71, 320.46, 322.22, 323.97, 325.75, 327.53, 329.32, 331.12, 332.93, 334.74, 336.57, 338.40, 340.24, 342.09,
343.95, 345.82, 347.69, 349.58, 351.47, 353.37, 355.28, 357.20, 359.13, 361.04, 362.98, 364.94, 366.90, 368.88,
370.86, 372.85, 374.85, 376.86, 378.88, 380.91, 382.94, 384.99, 387.05, 389.12, 391.19, 393.28, 395.38, 397.48,
399.60, 401.73, 403.86, 406.01, 408.15, 410.31, 412.49, 414.68, 416.87, 419.08, 421.30, 423.53, 425.76, 428.01,
430.27, 432.54, 434.82, 437.11, 439.41, 441.72, 444.05, 446.38, 448.73, 451.08, 453.45, 455.82, 458.21, 460.61,
463.02, 465.45, 467.88, 470.33, 472.78, 475.25, 477.73, 480.22, 482.73, 485.24, 487.77, 490.30, 492.85, 495.42,
497.99, 500.57)
def make_folder():
for folder in temp_set:
os.mkdir("nvt_%02d" % temp_set.index(folder))
print("%02d folders have been created!" % temp_set.index(folder))
def copy_files():
for mdp_file in temp_set:
shutil.copy("pr_nvt.mdp", "nvt_%02d/pr_nvt_%02d.mdp" % (temp_set.index(mdp_file), temp_set.index(mdp_file)))
print("%02d files have been copied to the folders!" % temp_set.index(mdp_file))
def edit_contents():
for temp_option in temp_set:
open_file = open("nvt_%02d/pr_nvt_%02d.mdp" % (temp_set.index(temp_option), temp_set.index(temp_option)), "r")
read_file = open_file.read()
edit_file = read_file.replace("300.00", "%.2f" % temp_option)
re_open_file = open("nvt_%02d/pr_nvt_%02d.mdp" % (temp_set.index(temp_option), temp_set.index(temp_option), "w")
save_file = re_open_file.write(edit_file)
if __name__ == "__main__":
make_folder()
copy_files()
edit_contents()
³»¿ë¼öÁ¤Çϴµ¥¼ ¸·ÇôÀÖ½À´Ï´Ù.
µû·Î ÆÄÀÏ ¼öÁ¤Çϴ°Ÿ¸ ¸¸µé¾î¼ Å×½ºÆ® ÇÒ¶§, open_file = open("pr_nvt.mdp", "r") ÀÌ·¸°Ô ÆÄÀÏ ÇÑ°³¸¸ ÁöÁ¤ÇÒ ¶§´Â µÇ¾ù´Âµ¥
¿©·¯°³¸¦ ó¸®ÇÏ·Á°í ¹Ù²Ù´Ï±î ¸·Çû½À´Ï´Ù.
°¨»çÇÕ´Ï´Ù.
¾Æ .. ¾²°íº¸´Ï .. Àú·±½ÄÀ¸·Î ÇÑ°³¸¸ ÁöÁ¤Çß´Ù´Â ¸»¾¸À̽ñº¿ä rÀº ÀÇ¹Ì ¾ø°í¿ä ..
r·Î ¸ÕÀú ÀоîµéÀÎ ´ÙÀ½¿¡ º¯°æÇÏ°í, ±×´ÙÀ½ w·Î ¿©´Ï±î Àüü ³»¿ëÀÌ À¯Áö µÇ¸é¼ ÇÊ¿äÇÑ ºÎºÐ¸¸ ¹Ù²¸¼ ÀúÀåÀÌ µÇ±æ·¡
ÀÌ·¸°Ô Çسù½À´Ï´Ù. Ãʺ¸¶ó¼ Àß ¸ð¸£°Ù¾î¿ä ¤Ð ¤¾¤¾