결론적으로 제가 하고 싶은 것부터 말씀드리고자 합니다.
fileloaction = "Securitypolicy.inf"
SecuritypolicyText = subprocess.run(["SECEDIT", "/export", "/cfg", fileloaction], capture_output=True, text=True)
위의 명령어를 통해 생성된 파일을 읽어 변수에 저장한 뒤에 xml 내용에 추가하는 작업니다.
질문
어떻게 해야 생성된 파일을 읽어올 수 있을까요? 고수님들의 답변 부탁드리겠습니다.
첫번째 방법
-------------------------------------------------------------------------------------------------------------------------------------
아래와 같이 파일을 생성합니다.
fileloaction = "Securitypolicy.inf"
SecuritypolicyText = subprocess.run(["SECEDIT", "/export", "/cfg", fileloaction], capture_output=True, text=True)
그리고 생성된 파일을 windows 명령어 type으로 파일을 읽어옵니다.
SecuritypolicyTextRead = subprocess.run(["type", fileloaction], capture_output=True, text=True)
근데 아래와 같이 오류가 나옵니다.
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Scripts\readsecurity.py", line 7, in <module>
SecuritypolicyTextRead = subprocess.run(["type", "Securitypolicy.txt"], capture_output=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
-------------------------------------------------------------------------------------------------------------------------------------
그래서 다른 방법으로 파일을 읽어 올려고 시도를 했습니다.
두번째 방법
-------------------------------------------------------------------------------------------------------------------------------------
아래와 같이 파일을 생성합니다.
fileloaction = "Securitypolicy.inf"
SecuritypolicyText = subprocess.run(["SECEDIT", "/export", "/cfg", fileloaction], capture_output=True, text=True)
파일을 읽어옵니다.
with open(fileloaction, 'r') as f:
content = f.read()
근데 아래와 같이 에러가 발생합니다.
ERROR:root:Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Scripts\Windwos_python_check3.py", line 203, in <module>
main()
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Scripts\Windwos_python_check3.py", line 115, in main
content = f.read()
^^^^^^^^
UnicodeDecodeError: 'cp949' codec can't decode byte 0xff in position 0: illegal multibyte sequence
그래서 인코딩 문제 같아 UTF-16 옵션을 주고 파일을 열었습니다.
fileloaction = "Securitypolicy.inf"
SecuritypolicyText = subprocess.run(["SECEDIT", "/export", "/cfg", fileloaction], capture_output=True, text=True)
# 생성된 파일을 읽어서 UTF-8로 인코딩하여 다시 쓰기
with open(fileloaction, 'r', encoding='utf-16') as f:
content = f.read()
근데 또 아래와 같은 에러가 발생합니다.
ERROR:root:Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Scripts\Windwos_python_check3.py", line 204, in <module>
main()
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Scripts\Windwos_python_check3.py", line 118, in main
SecuritypolicyTextRead = subprocess.run(["type", fileloaction], capture_output=True, text=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1026, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] 지정된 파일을 찾을 수 없습니다
---------------------------------------------------------------------------------------------------------------------------------------------------
댓글 0