我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用os.py()。
def execute_json(self, *params): """Execute the given batch of parameters and parse the JSON output. This method is similar to :py:meth:`execute()`. It automatically adds the parameter ``-j`` to request JSON output from ``exiftool`` and parses the output. The return value is a list of dictionaries, mapping tag names to the corresponding values. All keys are Unicode strings with the tag names including the ExifTool group name in the format <group>:<tag>. The values can have multiple types. All strings occurring as values will be Unicode strings. Each dictionary contains the name of the file it corresponds to in the key ``"SourceFile"``. The parameters to this function must be either raw strings (type ``str`` in Python 2.x, type ``bytes`` in Python 3.x) or Unicode strings (type ``unicode`` in Python 2.x, type ``str`` in Python 3.x). Unicode strings will be encoded using system's filesystem encoding. This behaviour means you can pass in filenames according to the convention of the respective Python version – as raw strings in Python 2.x and as Unicode strings in Python 3.x. """ params = map(fsencode, params) return json.loads(self.execute(b"-j", *params).decode("utf-8"))
def RegisterShellInfo(searchPaths): """Registers key parts of the Python installation with the Windows Shell. Assumes a valid, minimal Python installation exists (ie, SetupCore() has been previously successfully run) """ import regutil, win32con suffix = IsDebug() # Set up a pointer to the .exe's exePath = FindRegisterPythonExe("Python%s.exe" % suffix, searchPaths) regutil.SetRegistryDefaultValue(".py", "Python.File", win32con.HKEY_CLASSES_ROOT) regutil.RegisterShellCommand("Open", QuotedFileName(exePath)+" \"%1\" %*", "&Run") regutil.SetRegistryDefaultValue("Python.File\\DefaultIcon", "%s,0" % exePath, win32con.HKEY_CLASSES_ROOT) FindRegisterHelpFile("Python.hlp", searchPaths, "Main Python Documentation") FindRegisterHelpFile("ActivePython.chm", searchPaths, "Main Python Documentation") # We consider the win32 core, as it contains all the win32 api type # stuff we need. # FindRegisterApp("win32", ["win32con.pyc", "win32api%s.pyd" % suffix], searchPaths)
def whichCondaInstall(condaInstallBaseDir): '''takes a look at where os comes from, should be something like /reg/g/psdm/sw/conda/inst/miniconda2-dev-rhel7/envs/myenv/lib/os.py checks that this starts with the condaInstallBaseDir arg, then checks that inst is the next subdir, then returns what follows, i.e, returns miniconda2-dev-rhel7 for above ''' pythonLib = os.path.split(os.__file__)[0] if not pythonLib.startswith(condaInstallBaseDir): warning("whichMiniCondaInstall called for non central install, basedir arg=%s but os imports from %s" % (condaInstallBaseDir, pythonLib)) return 'non-central-miniconda-install' relpath = pythonLib.split(condaInstallBaseDir)[1] dirs = relpath.split(os.path.sep) if dirs[0]=='': dirs.pop(0) assert dirs[0]=='inst', "unexpected - central install conda dir layout has changed. Expected 'inst' at start of %s" % relpath return dirs[1]
def test_bug_18101(self): """Test that importing os.path in a file using the system python results in the right set of dependencies.""" # Set up the files for generate. fp = "usr/lib/python{0}/vendor-packages/pkg/client/indexer.py".format( py_ver_default) self.make_proto_text_file(fp, self.pyver_python_text.format( py_ver_default)) mp = self.make_manifest(self.pyver_test_manf_1_non_ex.format( py_ver=py_ver_default)) # Run generate and check the output. self.pkgdepend_generate("-d {0} {1}".format( self.test_proto_dir, mp)) self.check_res( self.make_pyver_python_res(py_ver_default, pkg5unittest.g_proto_area, fp, include_os=True).format( bin_ver=py_ver_default), self.output) self.check_res("", self.errout)
def execute(self, *params): """Execute the given batch of parameters with ``exiftool``. This method accepts any number of parameters and sends them to the attached ``exiftool`` process. The process must be running, otherwise ``ValueError`` is raised. The final ``-execute`` necessary to actually run the batch is appended automatically; see the documentation of :py:meth:`start()` for the common options. The ``exiftool`` output is read up to the end-of-output sentinel and returned as a raw ``bytes`` object, excluding the sentinel. The parameters must also be raw ``bytes``, in whatever encoding exiftool accepts. For filenames, this should be the system's filesystem encoding. .. note:: This is considered a low-level method, and should rarely be needed by application developers. """ if not self.running: raise ValueError("ExifTool instance not running.") self._process.stdin.write(b"\n".join(params + (b"-execute\n",))) self._process.stdin.flush() output = b"" fd = self._process.stdout.fileno() while not output[-32:].strip().endswith(sentinel): output += os.read(fd, block_size) return output.strip()[:-len(sentinel)]
def get_metadata_batch(self, filenames): """Return all meta-data for the given files. The return value will have the format described in the documentation of :py:meth:`execute_json()`. """ return self.execute_json(*filenames)
def get_metadata(self, filename): """Return meta-data for a single file. The returned dictionary has the format described in the documentation of :py:meth:`execute_json()`. """ return self.execute_json(filename)[0]
def get_tags(self, tags, filename): """Return only specified tags for a single file. The returned dictionary has the format described in the documentation of :py:meth:`execute_json()`. """ return self.get_tags_batch(tags, [filename])[0]
def LocatePythonCore(searchPaths): """Locate and validate the core Python directories. Returns a list of paths that should be used as the core (ie, un-named) portion of the Python path. """ import os, regutil currentPath = regutil.GetRegisteredNamedPath(None) if currentPath: presearchPaths = currentPath.split(";") else: presearchPaths = [os.path.abspath(".")] libPath = None for path in presearchPaths: if FileExists(os.path.join(path, "os.py")): libPath = path break if libPath is None and searchPaths is not None: libPath = LocatePath("os.py", searchPaths) if libPath is None: raise error("The core Python library could not be located.") corePath = None suffix = IsDebug() for path in presearchPaths: if FileExists(os.path.join(path, "unicodedata%s.pyd" % suffix)): corePath = path break if corePath is None and searchPaths is not None: corePath = LocatePath("unicodedata%s.pyd" % suffix, searchPaths) if corePath is None: raise error("The core Python path could not be located.") installPath = os.path.abspath(os.path.join(libPath, "..")) return installPath, [libPath, corePath]
def getPsanaReport(envA, envB, basedir): condaInstall = whichCondaInstall(basedir) relinfo = {} for env in [envA,envB]: pyfile = os.path.join(basedir, 'inst', condaInstall, 'envs', env, 'lib', 'python2.7', 'site-packages', 'anarelinfo', '__init__.py') assert os.path.exists(pyfile), "doesn't exist: %s" % pyfile relinfo[env]={} relinfo[env]['pyfile']=pyfile relinfo[env]['globals']={} relinfo[env]['locals']={} execfile(pyfile, relinfo[env]['globals'], relinfo[env]['locals']) pkgtags = {} for pkg, tag in relinfo[env]['locals']['pkgtags'].iteritems(): pkgtags[pkg]={'name':pkg, 'tag':tag} relinfo[env]['locals']['pkgtags'] = pkgtags report={'version':{}, 'tags':{'same':[],'new':[],'old':[],'changed':[]} } report['version']['old']=relinfo[envA]['locals']['version'] report['version']['new']=relinfo[envB]['locals']['version'] report['version']['same']=report['version']['old']==report['version']['new'] report['tags'] = compareDicts(relinfo[envA]['locals']['pkgtags'], relinfo[envB]['locals']['pkgtags']) return report
def test_bug_11805(self): """Test that paths as payloads work for file actions. This tests both that file actions with payloads have their dependencies analyzed correctly and that the correct path is used for internal dependency resolution.""" proto = pkg5unittest.g_proto_area tp = self.make_manifest(self.payload_manf) self.pkgdepend_generate("-d {0} {1}".format(proto, tp)) self.check_res(self.make_res_payload_1(proto, "usr/lib/python{0}/foo/bar.py".format(py_ver_default)), self.output) self.check_res("", self.errout)
def test_PEP_3149(self): """Test that Python 3 modules importing native modules can find them in the right place, following PEP 3149. On Solaris, this means 64-bit only, and with the "m" (pymalloc) flag turned on. """ # Create a python 3.x file that imports a native module. tp = self.make_manifest( self.pyver_test_manf_1.format(py_ver="3.4")) fp = "usr/lib/python{0}/vendor-packages/pkg/" \ "client/indexer.py".format("3.4") self.make_proto_text_file(fp, self.python3_so_text) # Run generate self.pkgdepend_generate("-m -d {0} {1}".format( self.test_proto_dir, tp)) # Take the output, split it into actions, and find exactly one # action which generated a correct dependency on zlib based on # indexer.py. pfx = base.Dependency.DEPEND_DEBUG_PREFIX acts = [ a for a in ( actions.fromstr(l) for l in self.output.strip().splitlines() ) if a.attrs.get(pfx + ".reason") == fp and "64/zlib.cpython-34m.so" in a.attrs[pfx + ".file"] ] self.assertTrue(len(acts) == 1) self.check_res("", self.errout)
def SetupCore(searchPaths): """Setup the core Python information in the registry. This function makes no assumptions about the current state of sys.path. After this function has completed, you should have access to the standard Python library, and the standard Win32 extensions """ import sys for path in searchPaths: sys.path.append(path) import os import regutil, win32api,win32con installPath, corePaths = LocatePythonCore(searchPaths) # Register the core Pythonpath. print corePaths regutil.RegisterNamedPath(None, ';'.join(corePaths)) # Register the install path. hKey = win32api.RegCreateKey(regutil.GetRootKey() , regutil.BuildDefaultPythonKey()) try: # Core Paths. win32api.RegSetValue(hKey, "InstallPath", win32con.REG_SZ, installPath) finally: win32api.RegCloseKey(hKey) # Register the win32 core paths. win32paths = os.path.abspath( os.path.split(win32api.__file__)[0]) + ";" + \ os.path.abspath( os.path.split(LocateFileName("win32con.py;win32con.pyc", sys.path ) )[0] ) # Python has builtin support for finding a "DLLs" directory, but # not a PCBuild. Having it in the core paths means it is ignored when # an EXE not in the Python dir is hosting us - so we add it as a named # value check = os.path.join(sys.prefix, "PCBuild") if "64 bit" in sys.version: check = os.path.join(check, "amd64") if os.path.isdir(check): regutil.RegisterNamedPath("PCBuild",check)
def SetupCore(searchPaths): """Setup the core Python information in the registry. This function makes no assumptions about the current state of sys.path. After this function has completed, you should have access to the standard Python library, and the standard Win32 extensions """ import sys for path in searchPaths: sys.path.append(path) import os import regutil, win32api,win32con installPath, corePaths = LocatePythonCore(searchPaths) # Register the core Pythonpath. print(corePaths) regutil.RegisterNamedPath(None, ';'.join(corePaths)) # Register the install path. hKey = win32api.RegCreateKey(regutil.GetRootKey() , regutil.BuildDefaultPythonKey()) try: # Core Paths. win32api.RegSetValue(hKey, "InstallPath", win32con.REG_SZ, installPath) finally: win32api.RegCloseKey(hKey) # Register the win32 core paths. win32paths = os.path.abspath( os.path.split(win32api.__file__)[0]) + ";" + \ os.path.abspath( os.path.split(LocateFileName("win32con.py;win32con.pyc", sys.path ) )[0] ) # Python has builtin support for finding a "DLLs" directory, but # not a PCBuild. Having it in the core paths means it is ignored when # an EXE not in the Python dir is hosting us - so we add it as a named # value check = os.path.join(sys.prefix, "PCBuild") if "64 bit" in sys.version: check = os.path.join(check, "amd64") if os.path.isdir(check): regutil.RegisterNamedPath("PCBuild",check)