导航:首页 > 编程语言 > pythonossyspath

pythonossyspath

发布时间:2022-04-27 07:31:39

1. python os.system()问题

if not os.path.exists(dst_dir): os.mkdir(dst_dir) print("this DIR %s set up Successful!" % dst_dir)else: print("this DIR %s is exists!" % dst_dir)zip_command = "winrar a %s %s" % (r_name,' '.join(source))# 这个winrar,你在cmd能运行吗?不能的话就加入绝对路径print (zip_command)try: os.system(zip_command) print("伤不起!")except Exception as e: print (e) print ("命令执行失败咯,你懂得!")

2. python sys.path.append('..')括号里这两个点是什么意思

目录的意思,即代表上一级目录。

3. python os和sys模块的区别

os与sys模块的官方解释如下:

os: This mole provides a portable way of using operating system dependent functionality.
这个模块提供了一种方便的使用操作系统函数的方法。
sys: This mole provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
这个模块可供访问由解释器使用或维护的变量和与解释器进行交互的函数。
总结就是,os模块负责程序与操作系统的交互,提供了访问操作系统底层的接口;sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境。
os 常用方法
os.remove(‘path/filename’) 删除文件
os.rename(oldname, newname) 重命名文件
os.walk() 生成目录树下的所有文件名
os.chdir('dirname') 改变目录
os.mkdir/makedirs('dirname')创建目录/多层目录
os.rmdir/removedirs('dirname') 删除目录/多层目录
os.listdir('dirname') 列出指定目录的文件
os.getcwd() 取得当前工作目录
os.chmod() 改变目录权限
os.path.basename(‘path/filename’) 去掉目录路径,返回文件名
os.path.dirname(‘path/filename’) 去掉文件名,返回目录路径
os.path.join(path1[,path2[,...]]) 将分离的各部分组合成一个路径名
os.path.split('path') 返回( dirname(), basename())元组
os.path.splitext() 返回 (filename, extension) 元组
os.path.getatime\ctime\mtime 分别返回最近访问、创建、修改时间
os.path.getsize() 返回文件大小
os.path.exists() 是否存在
os.path.isabs() 是否为绝对路径
os.path.isdir() 是否为目录
os.path.isfile() 是否为文件
sys 常用方法
sys.argv 命令行参数List,第一个元素是程序本身路径
sys.moles.keys() 返回所有已经导入的模块列表
sys.exc_info() 获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息
sys.exit(n) 退出程序,正常退出时exit(0)
sys.hexversion 获取Python解释程序的版本值,16进制格式如:0x020403F0
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.maxunicode 最大的Unicode值
sys.moles 返回系统导入的模块字段,key是模块名,value是模块
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称
sys.stdout 标准输出
sys.stdin 标准输入
sys.stderr 错误输出
sys.exc_clear() 用来清除当前线程所出现的当前的或最近的错误信息
sys.exec_prefix 返回平台独立的python文件安装的位置
sys.byteorder 本地字节规则的指示器,big-endian平台的值是'big',little-endian平台的值是'little'
sys.right 记录python版权相关的东西
sys.api_version 解释器的C的API版本
sys.stdin,sys.stdout,sys.stderr
stdin , stdout , 以及stderr 变量包含与标准I/O 流对应的流对象. 如果需要更好地控制输出,而print 不能满足你的要求, 它们就是你所需要的. 你也可以替换它们, 这时候你就可以重定向输出和输入到其它设备( device ), 或者以非标准的方式处理它们
我们常用print和raw_input来进行输入和打印,那么print 和 raw_input是如何与标准输入/输出流建立关系的呢?
其实Python程序的标准输入/输出/出错流定义在sys模块中,分别 为: sys.stdin,sys.stdout, sys.stderr
下列的程序也可以用来输入和输出是一样的:
import sys

sys.stdout.write('HelloWorld!')

print 'Please enter yourname:',
name=sys.stdin.readline()[:-1]
print 'Hi, %s!' % name

那么sys.stdin, sys.stdout, stderr到底是什么呢?我们在Python运行环境中输入以下代码:
import sys
for f in (sys.stdin,sys.stdout, sys.stderr): print f
输出为:
<open file'<stdin>', mode 'r' at 892210>
<open file'<stdout>', mode 'w' at 892270>
<open file'<stderr>', mode 'w at 8922d0>

由此可以看出stdin, stdout, stderr在Python中无非都是文件属性的对象,他们在Python启动时自动与Shell 环境中的标准输入,输出,出错关联。
而Python程序的在Shell中的I/O重定向与本文开始时举的DOS命令的重定向完全相同,其实这种重定向是由Shell来提供的,与Python 本身并无关系。那么我们是否可以在Python程序内部将stdin,stdout,stderr读写操作重定向到一个内部对象呢?答案是肯定的。
Python提供了一个StringIO模块来完成这个设想,比如:
from StringIO import StringIO
import sys
buff =StringIO()

temp =sys.stdout #保存标准I/O流
sys.stdout =buff #将标准I/O流重定向到buff对象
print 42, 'hello', 0.001

sys.stdout=temp #恢复标准I/O流
print buff.getvalue()
阅读全文

4. python的os.system问题 我在环境变量里面设置了win.rar的path,(比如我的winrar在c:\winrar\)

Windows需要添加系统环境变量才能在任何路径下访问环境变量路径下的程序或命令,添加环境变量后需要记得查看PATHEXT是否包含对应的程序后缀
如增加了python后,想要在任何路径下执行py程序,需要添加Python系统path环境变量,同时增加.PY的path可执行后缀,否则无法识别.py程序及里面的命令,如果.py程序在D盘下,就不能使用C盘的WinRAR命令,提示winrar不是内部控制程序。

5. python os.system 命令求助

看一下是不是linux系统权限的问题。确保你对那个目录有读写权限。另外,如果是想一个指令建立多级目录,需要用-p参数。 mkdir -p PATH

6. Python 3 中 os.system调用问题

ifnotos.path.exists(dst_dir):

os.mkdir(dst_dir)
print("thisDIR%ssetupSuccessful!"%dst_dir)

else:
print("thisDIR%sisexists!"%dst_dir)

zip_command="winrara%s%s"%(r_name,''.join(source))
#这个winrar,你在cmd能运行吗?不能的话就加入绝对路径
print(zip_command)

try:
os.system(zip_command)

print("伤不起!")
exceptExceptionase:

print(e)

print("命令执行失败咯,你懂得!")

7. python中os.system函数可以执行export命令吗

返回0是执行成功了,但是你的命令里面的错误是无法收到的

比如os.system('aaa')返回0,表示执行了aaa这个命令
但是可能系统根本没有aaa这个命令

如果你要获得详细的运行结果,建议使用popen命令,如下例子:
prog = subprocess.Popen("mysql -usanguo -psgtest -f <%s"%sqlFilePath, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True)
out,error= prog.communicate()
if prog.returncode:
print 'File import error'
return [False,error]

8. 关于在python中使用os.system运行批处理.bat文件

第一个执行后并没有结果,因为随着执行返回,当前目录就改变了。
第二个可以切换当前目录。
第三个跟第二个配合使用应该可以。或者用 os.path.join(DstDir,'test.bat') 来直接引用绝对路径

9. python中用filedialog.askdirectory获取文件夹路径后用os.system函数打开失败,因为斜杠问题。如何解决

os.system('explorer'+os.path.normpath(savefolder))

使用os.path.normpath格式化路径

10. 如何用Python os.path.walk方法遍历搜索文件内容的操作详解

文中使用到了Python os模块和Python sys模块,这两个模块具体的使用方法请参考玩蛇网相关文章阅读。
Python os.path.walk方法遍历文件搜索内容方法代码如下:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

import os, sys
#代码中需要用到的方法模块导入

listonly = False

skipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb'] # ignore binary files

def visitfile(fname, searchKey):
global fcount, vcount
try:
if not listonly:
if os.path.splitext(fname)[1] in skipexts:
pass
elif open(fname).read().find(searchKey) != -1:
print'%s has %s' % (fname, searchKey)
fcount += 1
except: pass
vcount += 1

#www.iplaypy.com

def visitor(args, directoryName,filesInDirectory):
for fname in filesInDirectory:
fpath = os.path.join(directoryName, fname)
if not os.path.isdir(fpath):
visitfile(fpath,args)

def searcher(startdir, searchkey):
global fcount, vcount
fcount = vcount = 0
os.path.walk(startdir, visitor, searchkey)

if __name__ == '__main__':
root=raw_input("type root directory:")
key=raw_input("type key:")
searcher(root,key)
print 'Found in %d files, visited %d' % (fcount, vcount)

阅读全文

与pythonossyspath相关的资料

热点内容
linux安装xdm 浏览:736
java计算12 浏览:249
大金空调摆动式压缩机 浏览:453
新的云服务器如何设置首页 浏览:687
javastring字符位置 浏览:196
银河麒麟字体库存在哪个文件夹 浏览:956
魔兽加丁服务器的航空叫什么 浏览:152
花冠改装案例哪个app多 浏览:515
成绩单app哪个好用 浏览:140
北美程序员vs国内程序员 浏览:181
php解析xml文档 浏览:121
石墨文档APP怎么横屏 浏览:185
墙主钢筋加密和非加密怎么看 浏览:144
金山区文件夹封套定制 浏览:708
soho程序员 浏览:672
java字节截取 浏览:526
php提交作业 浏览:815
房产还没解压可以办理赠予吗 浏览:224
java毫秒转分钟 浏览:753
模式识别中文pdf 浏览:774