python shell是什么东西

Python08

python shell是什么东西,第1张

pythonshell是Python的命令行。

shell中最常用的是ls命令,python对应的写法是:os.listdir(dirname),这个函数返回字符串列表,里面是所有的文件名,不过不包含”.”和”..”。

如果要遍历整个目录的话就会比较复杂一点,在解释器里试一下:

>>>os.listdir(”/”)

[’tmp’,‘misc’,‘opt’,‘root’,‘.autorelabel’,’sbin’,’srv’,‘.autofsck’,‘mnt’,‘usr’,‘var’,‘etc’,’selinux’,‘lib’,‘net’,‘lost+found’,’sys’,‘media’,‘dev’,‘proc’,‘boot’,‘home’,‘bin’]

就像这样,接下去所有命令都可以在python的解释器里直接运行观看结果。

扩展资料:

pythonshell对应于shutil.copy(src,dest),这个函数有两个参数,参数src是指源文件的名字,参数dest则是目标文件或者目标目录的名字。

如果dest是一个目录名,就会在那个目录下创建一个相同名字的文件。与shutil.copy函数相类似的是shutil.copy2(src,dest),不过copy2还会复制最后存取时间和最后更新时间。

不过,shell的cp命令还可以复制目录,python的shutil.copy却不行,第一个参数只能是一个文件。

其实,python还有个shutil.copytree(src,dst[,symlinks])。参数多了一个symlinks,它是一个布尔值,如果是True的话就创建符号链接。

移动或者重命名文件和目录,shutil.move(src,dst),与mv命令类似,如果src和dst在同一个文件系统上,shutil.move只是简单改一下名字,如果src和dst在不同的文件系统上,shutil.move会先把src复制到dst,然后删除src文件。

参考资料:Python—百度百科

python shell不是特指某一项命令,而是一种命令行环境。可以在shell里面导包、执行语句,常见的有 ipython环境,比python自带的shell要好得多。安装方式:pip install ipython

python shell打开的方式如下:

使用class subprocess.Popen(command,shell=True)。Popen类有Popen.stdin,Popen.stdout,Popen.stderr三个有用的属性,可以实现与子进程的通信。

将调用shell的结果赋值给python变量

handle = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)

实例:

handle = subprocess.Popen('ls -l', stdout=subprocess.PIPE, shell=True)

handle = subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE, shell=True)

handle = subprocess.Popen(args='ls -l', stdout=subprocess.PIPE, shell=True) 

print handle.stdout.read()

print handle.communicate()[0]