3

I have been struggling a lot with this piece of code i wrote by referring tutorials:

    import os
    os.chdir(f'/home/django/image-io/accounts/john_doe/optimized/')
    cwd = os.getcwd()
    logger.info(f'CURRENT WORKING DIRECOTRY :{cwd}')
    subprocess.Popen(f"optimize-images")

What i am trying to do is dynamically change directory on username (john_doe) in this case and run a command optimize-images inside that directory to optimize all images .

It doesn't seem to work.

When i checked the logs , it says:

optimize-images : "No such directory present"

I am not able to understand , where i am doing wrong. any help would be appreciated.

2 Answers2

1

In this code, We are optimizing the images contained in the optimized folder.

import os
import subprocess
os.chdir(r'/home/django/image-io/accounts/john_doe/optimized')
cwd = os.getcwd()
p=subprocess.Popen([r"optimize-images", cwd])

We need to pass the the parameters in the array list format. You can get more details here.

subprocess.Popen([r"optimize-images", cwd])

Note:

  • r is used to indicate the raw string.
  • f is used when we need to include the value of Python expressions inside strings.
Kirpal R
  • 81
  • 3
  • Thanks for your answer. did it work for you ? i mean locally. – shriniket deshmukh Jul 02 '21 at 08:20
  • Yeah. I created a python script with above lines and then executed it. Check your path whether it is Absolute path or Relative Path when passing the parameter to os.chdir() function. – Kirpal R Jul 02 '21 at 08:24
  • 1
    It applies Lossless data compression Algorithm on Image to get it optimized for web usage. – Kirpal R Jul 02 '21 at 09:35
  • Thanks. Another question just popped in my mind. I am using the image optimization code for a long running task where diffrent people will be calling this endpoint. Will it have any impact or all the subprocesses will be managed seperately. ? – shriniket deshmukh Jul 02 '21 at 12:15
0

Is this what you need? Popen error: [Errno 2] No such file or directory

Try subprocess.Popen(f"optimize-images", shell = True)

Peter
  • 100
  • 10