import os是什么库(求python代码!!)

本文目录
求python代码!!
import os
# 首先,使用os模块的listdir函数获取“现代汉语语料库”的所有文件名称,并将其存放到一个列表中:
file_list = os.listdir(’现代汉语语料库’)
# 然后,使用random模块的randint函数取整后随机抽取5个样本:
sample_list = random.sample(file_list, 5)
# 最后,使用open函数打开这5个样本的文件,并将其内容合并到一个新文件中:
with open(’new_file.txt’, ’w’) as f:
for sample in sample_list:
with open(sample, ’r’) as sf:
f.write(sf.read())
def whatToDo(name, day):
if day in :
return name + "在星期" + day + "看电影"
else:
return name + "在星期" + day + "学习"
python 复制文件
用Python把某一目录下的文件复制到指定目录中,代码如下:
1、首先插入必要的库:
import os
import os.path
import shutil
import time, datetime
2、实现复制文件代码如下:
def copyFiles(sourceDir,targetDir):
if sourceDir.find(".svn") 》 0:
return
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir,file)
targetFile = os.path.join(targetDir,file)
if os.path.isfile(sourceFile):
if not os.path.exists(targetDir):
os.makedirs(targetDir)
if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
open(targetFile,"wb").write(open(sourceFile,"rb").read())
if os.path.isdir(sourceFile):
First_Directory = False
copyFiles(sourceFile, targetFile)
3、主函数的实现:
if __name__ =="__main__":
print "Start(S) or Quilt(Q) \n"
flag = True
while (flag):
answer = raw_input()
if ’Q’ == answer:
flag = False
elif ’S’== answer :
formatTime = getCurTime()
targetFoldername = "Build " + formatTime + "-01"
Target_File_Path += targetFoldername
copyFiles(Debug_File_Path,Target_File_Path)
removeFileInFirstDir(Target_File_Path)
coverFiles(Release_File_Path,Target_File_Path)
moveFileto(Firebird_File_Path,Target_File_Path)
moveFileto(AssistantGui_File_Path,Target_File_Path)
writeVersionInfo(Target_File_Path+"\\ReadMe.txt")
print "all sucess"
else:
print "not the correct command"
Linux里面import os作用是什么
import os
print(os.path.abspath(".")) #当前目录的绝对路径
print(os.path.abspath(r"..")) #上级目录的绝对路径 print(os.path.abspath(r"D:\python_workshop\python6\revise\函数.py"))
运行结果
D:\python_workshop\python6\selenium_webdriver
D:\python_workshop\python6
D:\python_workshop\python6\revise\函数.py
其他的一些常见函数:
1、os.getcwd()函数
功能:获取当前目录,python 的工作目
import os
pwd = os.getcwd()
print (pwd)
2、os.name 函数
功能:获取当前使用的操作系统(获取信息不够详细)
其中 ’nt’ 是 windows,’posix’ 是 linux 或者 unix
import os
name = os.name
if name == ’posix’:
print ("this is Linux or Unix")
elif name == ’nt’:
print ("this is windows")
else:
print ("this is other system")
3、os.remove()函数
功能:删除指定文件
eg:删除 file.txt 文件
import os
os.remove(’file.txt‘)
4、os.removedirs()函数
功能:删除指定目录
eg:删除 file目录
import os
os.removedirs(‘file’)
5、os.system()函数
功能:运行shell命令
eg:执行ls -a 》 1.txt命令
import os
os.system(‘ls -a 》 1.txt’)
6、os.mkdir()函数
功能:创建一个新目录
eg:创建一个 file 目录
import os
os.mkdir(‘file’)
7、os.chdir()函数
功能:改变当前路径到指定路径
eg:我现在从当前路径到 filepath 所指定的路径下
import os
filepath = ’/home’
pwd = os.getcwd()
print (pwd)
os.chdir(filepath)
pwd = os.getcwd()
print (pwd)
8、os.listdir()函数
功能:返回指定目录下的所有目录和文件
eg:列出当前目录下的所有文件和目录
import os
pwd = os.getcwd()
name = os.listdir(pwd)
for filename in name:
print (filename)

更多文章:
myeclipse激活了还是要激活(myeclipse为什么要激活)
2026年7月3日 22:30
phpcms 自定义字段标签模型(phpcms建议PHPCMS在评论功能上进行扩展,实现评论自定义字段)
2025年10月13日 22:30
android根据源码解决问题(如何解决android ndk r8c 老是重新编译源代码的问题)
2026年3月30日 00:30
selectorgadget下载(bootntrselector加载不成功)
2025年9月21日 13:30
如何输入string类型字符串(博图画面中string类型怎么输入)
2026年5月6日 23:15
format函数千位分隔符(15、pandas的设置数字格式,小数位数、百分号、千位分隔符)
2026年4月6日 17:30
c++多线程并发(C++的多线程里的事件是怎样定义的,事件和线程有啥联系和区别)
2026年9月20日 11:45
停止天然砂出口台湾(急求中国2010年10月11日起禁止出口的货物目录)
2025年6月19日 16:00















