Python Vs C - Processing Environment Variable on Windows

I see a variation in output between C and Python code on Windows, trying to get the same functionality. The c code (similar to unix shell scripts) shows the TEST1 environment variable in "test.bat.output" with its value as an empty string, while the python code removes this environment variable.

Is there a way to ask Python not to remove the environment variable from the environment table when it is empty?

FROM

#include <windows.h>

main()
{

  DWORD dwRet;
  char pszOldVal[1024] = "abc";

  if(! SetEnvironmentVariable("TEST1", ""))  
    puts("Error\n");

  // _putenv("TEST1=");

  // GetEnvironmentVariable("TEST1", pszOldVal, dwRet);
  system("cmd /c test.bat >test.bat.output");
}

      

Python

import os
os.environ['TEST1'] = ""
os.environ['TEST2'] = "karthik"
os.system("cmd /c test.bat > test.bat.output.python")

      

-Karthik

+2


source to share


3 answers


This is a possible answer. I don't have a Windows system to test with, so I really don't know what this code will do, but what happens if you do:

import os, subprocess
myenv = {}
myenv.update(os.environ)
myenv['TEST1'] = ""
myenv['TEST2'] = "karthik"
subprocess.Popen(('cmd', '/c', 'test.bat'), stdout=file("test.bat.output.python", 'w'),
                 env=myenv).wait()

      



In my opinion, you might encounter a Python error. Especially if the code I just gave works.

Plus, testing your code on Unix actually works. Environment ends up with an empty environment variable.

0


source


Cross-platform compatibility between Windows and "most of the rest" (operating systems derived or inspired by Unix) is often difficult to obtain, especially in the myriad cases that inevitably arise (like in this question) an environment variable to empty means it is reset " Sometimes it's just easier to access specific Windows functionality directly rather than trying to stretch "cross-platform" functionality.



While the traditional way of accessing Windows functionality from Python is win32all , in recent versions of Python, the ctypes standard library module offers an alternative in order not to require the installation of C-coded extensions. An interesting project is jaraco.windows , a collection of pure Python code on top ctypes

to make Windows operations easier and smoother. For example, if you are working with the environment and / or registry, the environ.py module offers a nice set of functions and classes with more Pythonic feel than the simple win32 API accessed by the underlyingctypes

(for example, get an exception with a readable error message in it in case of errors, instead of checking for return codes and c).

+2


source


Yes empty values ​​don't fit into the environment, but the interesting thing is calling SetEnvironmentVariable from win32api or the ctypes module, has the same effect as os.environ, although win32api.SetEnvironmentVariable will call the same function as in C.

So are you sure you are getting a different result in the C code?

import win32api
import os
win32api.SetEnvironmentVariable("TEST1", "")
# or 
# import ctypes
# ctypes.windll.kernel32.SetEnvironmentVariable("TEST1", "")
os.system("echo %TEST1%")

      

0


source







All Articles