2017-12-22 16 views
0

내 컴퓨터에 보이지 않는 폴더를 만들려고합니다.설정된 위치에 보이지 않는 폴더 만들기

내 하위 폴더가 삭제되면 업데이트되지 않는 것이 문제입니다.

새 폴더를 추가하려는 경우 안전하지 않을 때마다 C :/TVBA를 삭제하지 않으면 업데이트되지 않습니다.

내 파이썬 스크립트가있는 보이지 않는 폴더와 C:/TVBA도 생성합니다.

어떻게 수정합니까?

try: 
     rootpath = r'C:/TVBA' 
     os.mkdir(rootpath) 
     os.chdir(rootpath) 
    except OSError: 
     pass 
    for sub_folder in ['1', '2', '3', '4', '5', '6']: 
     try: 
      os.mkdir(sub_folder) 
      ctypes.windll.kernel32.SetFileAttributesW(sub_folder, 2) 
     except OSError: 
      pass 

답변

0

ctypes.windll.kernel32.SetFileAttributesW()에 필수 경로를 전달하지 않았기 때문에 현재 폴더에 보이지 않는 폴더가 만들어졌습니다. 내 파이썬 버전은 3.6이며, 나는 윈도우 10에서 코드 아래 시도 :

import os 
import ctypes 

# Create a folder, make sub_folders in it and hide them 
try: 
    rootpath = "path/to/folder" 
    os.mkdir(rootpath) 
except OSError as e: 
    print(e) # So you'll know what the error is 

for subfolder in ['1', '2', '3', '4', '5', '6']: 
    try: 
     path = rootpath + "/" + subfolder # Note, full path to the subfolder 
     os.mkdir(path) 
     ctypes.windll.kernel32.SetFileAttributesW(path, 2) # Hide folder 
    except OSError as e: 
     print(e) 


# Remove a subfolder 
os.rmdir(rootpath + "/" + "1") 

# Add a new sub_folder 
path = rootpath + "/" + "newsub" 
os.mkdir(path) 

# Hide the above newsub 
ctypes.windll.kernel32.SetFileAttributesW(path, 2) 

# Unhide all the sub-folders in rootpath 
subfolders = os.listdir(rootpath) 

for sub in subfolders: 
    ctypes.windll.kernel32.SetFileAttributesW(rootpath + "/" + sub, 1) 

위의 코드를 실행 한 후, rootpath의 하위 폴더는 다음과 같습니다 도움이

>>> os.listdir(rootpath) 
['2', '3', '4', '5', '6', 'newsub'] 

희망을.

0

os.makedirs를 사용하십시오. 관련 문서는

"os.makedirs입니다

서명 : os.makedirs (이름, 모드 = 511, exist_ok = 거짓) 참조 문 : makedirs (이름 [모드 = 0o777] [exist_ok = 거짓 ])

잎 디렉토리와 모든 중간을 만들. 존재하지 않는 경우 대상 디렉토리가 이미 이있는 경우뿐만 아니라 오른쪽) . 생성됩니다 (중간 경로 세그먼트 것을 제외하고, MKDIR처럼 작동, exists_ok가 False 인 경우 OSError를 발생시키고 예외가 발생하면 예외는 발생하지 않습니다. 그는 재귀 적이다. "

+0

예를 보여 줄 수 있습니까? –