Search code examples
pythondirectorysubprocess7zip

Python error: Cannot find the file specified


This is my body of code:

os.chdir("C:\\Users\\Desktop")

rc = subprocess.call(['7z', 'a', 'test', '-y', 'myarchive.zip'] +
                     [r'device teams.txt'])

It gives me an error pointing to r'device teams.txt' saying the specified file does not exist.

I checked the directory and it is in the desktop directory so I am not sure why it is giving me this error


Solution

  • Based on your comments, the problem isn't the txt file path, it's that the command 7z cannot be found. You can check this by just calling rc = subprocess.call(['7z']): the error The system cannot find the file specified persists.

    Here's how you can achieve the same thing using PowerShell for example:

    import os
    import subprocess
    os.chdir("C:\\Users\\Username\\Desktop")
    rc = subprocess.call("powershell Compress-Archive -Path 'device teams.txt' -DestinationPath archive.zip")