草庐IT

os.system

全部标签

python - 有人可以向我解释以下 os.fork() 示例吗?

[代码取自MarkLutz编写的ProgrammingPython4thEdition]"forkschildprocessesuntilyoutype'q'"importosdefchild():print('Hellofromchild',os.getpid())os._exit(0)#elsegoesbacktoparentloopdefparent():whileTrue:newpid=os.fork()ifnewpid==0:child()else:print('Hellofromparent',os.getpid(),newpid)ifinput()=='q':breakp

python - 在 python 中检查区分大小写的 os.path.isfile(filename)

我需要检查给定的文件是否存在,并且区分大小写。file="C:\Temp\test.txt"ifos.path.isfile(file):print"exist..."else:print"notfound..."TEST.TXT文件位于C:\Temp文件夹下。但是显示文件“C:\Temp\test.txt”的“文件存在”输出的脚本应该显示“未找到”。谢谢。 最佳答案 改为列出目录中的所有名称,以便进行区分大小写的匹配:defisfile_casesensitive(path):ifnotos.path.isfile(path):r

python - 与 os.path.commonprefix 相反

os.path.commonprefix的反义词是什么?我有两条路径,我想要不重叠的路径,例如:>>>p1='/Users/foo/something'>>>p2='/Users/foo/something/else/etc'>>>printsomefunction([p1,p2])'/else/etc' 最佳答案 >>>p1='/Users/foo/something'>>>p2='/Users/foo/something/else/etc'>>>os.path.relpath(p2,start=p1)'else/etc'正确答案

python - 如何使用 Homebrew 在 OS X 上安装 Python 3.3(不是 3.4)?

如果您通过Homebrew安装python3,它会默认安装最新版本,我也这样做了。但我想要3.3而不是3.4。如何专门使用Homebrew将其替换为3.3?我想尝试使用Python3的Django,但我只是在学习Django,所以我想使用最新的稳定版本,目前是1.6,它与Python3.3兼容。我想在Python3中使用它,所以它必须是3.3。Django1.7与Py3.4兼容,但我不想弄乱它直到它稳定......在OSX10.8.5上 最佳答案 您可以从homebrewFAQ中拼凑出一些元素.CanIeditformulaemys

python - 如何在不被阻塞的情况下读取 os.pipe()?

我正在尝试从一个打开的os.pipe()中读取,以查看在读取时它是否为空。问题是调用read()会导致程序在那里阻塞,直到那里实际上有东西要读,但是如果我正在做的测试成功的话,就不会有任何东西了。我知道我可以使用带有超时的select.select()但是我想知道是否有其他解决方案。 最佳答案 你可以试试这个。importos,fcntlfcntl.fcntl(thePipe,fcntl.F_SETFL,os.O_NONBLOCK)有了这个thePipe.read()应该是非阻塞的。来自pipe(7)手册页:Ifaprocessat

python - os.exec* 的第一个参数

来自python文档:Thevariousexec*()functionstakealistofargumentsforthenewprogramloadedintotheprocess.Ineachcase,thefirstoftheseargumentsispassedtothenewprogramasitsownnameratherthanasanargumentausermayhavetypedonacommandline.FortheCprogrammer,thisistheargv[0]passedtoaprogram’smain().Forexample,os.execv

python /Matplotlib : convert Axis ⇔ Data coordinates systems

我的问题很简单:在matplotlib中,如何轻松地将轴系统中的坐标与数据系统进行转换(理想情况下,我正在寻找一个简单的函数output_coords=magic_func(input_coords))实际上我的确切问题是:我想绘制一个matplotlib.patches.Ellipse,其中心在Axis系统中,但其大小(宽度和长度)在Data系统中。但是transforms.blended_transform_factory方法在这种情况下不起作用。谢谢! 最佳答案 要从Axes实例ax获取转换,您可以使用axis_to_data

python - 如何在 Python 中使用 os.makedirs 进行错误验证?

如何使此命令验证错误?ifblablablabla:os.makedirs('C:\\test\\')如果文件夹已经存在,他返回一个错误...我怎样才能让它忽略这个错误?继续前进? 最佳答案 try:os.makedirs('C:\\test\\')exceptOSError:pass您可能还想检查特定的“已存在”错误(因为OSError可能意味着其他事情,例如permissiondenied...importerrnotry:os.makedirs('C:\\test\\')exceptOSErrorase:ife.errno!=

Python 在 Windows 上的 os.listdir 行为

>>>importos>>>os.chdir('c:/python27')>>>os.listdir('c:')['Tools','include','python.exe','libs','DLLs','Lib','NEWS.txt','w9xpopen.exe','Doc','pythonw.exe','LICENSE.txt','README.txt','tcl']>>>os.listdir('c:/')['users','ProgramFiles','Python27','windows']为什么“c:”后的“/”会影响结果?有没有办法让os.listdir('c:')返回“c

python - ImportError : No module named gi. 存储库 Mac OS X

我正在MacOSX上安装virt-manager0.10.0首先我用homebrew安装了python、libvirt、gtk+3、pygtk和其他依赖但是当我运行virt-manager时出现了这个错误fromgi.repositoryimportGObjectImportError:Nomodulenamedgi.repository当我在python命令行中运行这个导入时,我得到了同样的错误。但是当我尝试importgtk时没有错误我认为问题是Homebrew的pygtk版本不使用gtk+3而使用gtk2,正如我们在这里看到的gi.repositoryWindows只有gtk+3