2
import os
finalFilePath = "C:\Windows\System32\inetsrv\config\applicationHost.config"
with open(finalFilePath) as wsfinalfile_document:
            openFinalFile = wsfinalfile_document.read()
            print(openFinalFile)

When the code is ran, there is no error but there is no output, however, when I move the applicaitonHost.config file to a not hidden directory and rerun the code, it printed the content inside applicationHost.config.

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
Javier
  • 23
  • 3
  • 3
    The backslash is an escape character try double \\ or put an r in front of the string like this r"C:\Windows\System32\inetsrv\config\applicationHost.config" – Mace May 23 '20 at 08:47
  • Also worth noting that [PEP 8](https://www.python.org/dev/peps/pep-0008/) advises the use of `snake_case` rather than `camelCase`. – lpd11 May 23 '20 at 09:03
  • It is better to use forward slashes for a [Windows path in Python](https://stackoverflow.com/questions/2953834/) anyway. – Karl Knechtel Aug 07 '22 at 05:19

2 Answers2

0

Like @Mace mentioned, you have to escape all the backslashes because when Python seems only one backslash it interprets it as an escape sequence Here is the code you want to use, which has the backslashes escaped so that Python sees it has a literal backslash:

import os
finalFilePath = "C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config"
with open(finalFilePath) as wsfinalfile_document:
    openFinalFile = wsfinalfile_document.read()
    print(openFinalFile)
Siddharth Dushantha
  • 1,391
  • 11
  • 28
  • I have tried adding both the r at the front of the string and also using double backslash but there is still no output given. Any idea where the issue is? – Javier May 23 '20 at 14:00
0

After correcting the backslash problem you still get no output so I have tested your code. I have no inetsrv config so I have tested it on powershell.exe.config, also in a System32 subdirectory. Your code ran ok and produced output.

The only reasons I can come up with for not generating output are:

1-no python adminstrator rights for a System32 subdirectory

2-non existing file

3-the hidden directory you mentioned

4-the file is empty

The first 3 reasons should produce an error/exception if not allowed.

The test program:

import os

# finalFilePath = r"C:\Windows\System32\inetsrv\config\applicationHost.config"

finalFilePaths = [
    r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config",
    r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - empty.config",
    r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - non existing.config",
    r"C:\test\hidden\powershell.exe.config",
    r"C:\test\hidden\powershell.exe - empty.config",
    r"C:\test\hidden\powershell.exe - non existing.config",
    r"C:\test\non hidden\powershell.exe.config",
    r"C:\test\non hidden\powershell.exe - empty.config",
    r"C:\test\non hidden\powershell.exe - non existing.config",
    ]

for finalFilePath in finalFilePaths:

    print(f'START: {finalFilePath}')

    if not os.path.exists(finalFilePath):
        print(f'ERROR: file not found {finalFilePath}')

    try:
        with open(finalFilePath) as wsfinalfile_document:
                    openFinalFile = wsfinalfile_document.read()
                    print(openFinalFile)
    except Exception as e:
        print(f'EXCEPTION: {str(e)}')

    print('END -----------------------')

Testing shows that only reason 2 generates an exception. The other are allowed. The only way the test program generates no output/exception is if the file is empty.

START: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config
<configuration>
...
</configuration>

END -----------------------
START: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - empty.config

END -----------------------
START: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - non existing.config
ERROR: file not found C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - non existing.config
EXCEPTION: [Errno 2] No such file or directory: 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe - non existing.config'
END -----------------------


START: C:\test\hidden\powershell.exe.config
<configuration>
...
</configuration>

END -----------------------
START: C:\test\hidden\powershell.exe - empty.config

END -----------------------
START: C:\test\hidden\powershell.exe - non existing.config
ERROR: file not found C:\test\hidden\powershell.exe - non existing.config
EXCEPTION: [Errno 2] No such file or directory: 'C:\\test\\hidden\\powershell.exe - non existing.config'
END -----------------------



START: C:\test\non hidden\powershell.exe.config
<configuration>
...
</configuration>

END -----------------------
START: C:\test\non hidden\powershell.exe - empty.config

END -----------------------
START: C:\test\non hidden\powershell.exe - non existing.config
ERROR: file not found C:\test\non hidden\powershell.exe - non existing.config
EXCEPTION: [Errno 2] No such file or directory: 'C:\\test\\non hidden\\powershell.exe - non existing.config'
END -----------------------

Process finished with exit code 0
Mace
  • 1,355
  • 8
  • 13