python文件夹下文件
获取文件夹下所有名称的方法:
把软件程序运行之后,选择添加文件名称管理。
在里面将所有的数据信息进行选定,然后点击获取。
Ⅱ 用python把文件夹下的所有文件包括文件夹里面的文件都拷贝到同一个目录下
importos
importshutil
defwenjian(path):
ifos.path.isfile(path):
shutil.(path,'c:\new_dir')
ifos.path.isdir(path):
lists=os.listdir(path)
foriinlists:
wenjian(i)
foriinos.walk('c:\1'):
wenjian(i)
建议你把检索到的文件都放到一个新的文件夹里,要不然系统在同一个文件夹里不停的读取和写入可能会陷入死循环以至出错。
Ⅲ python里怎么列出一个文件夹下面的所有文件夹和文件
先import os
然后用files=os.listdir(path)即可得到一个文件夹path下面的所有文件夹和文件
Ⅳ python获取文件夹下所有文件名
使用os模块可以获取指定文件夹下所有文件名,有两个方法os.walk()和os.listdir().
(1)os.walk可以用于遍历指定文件下所有的子目录、非目录子文件。
(2)os.listdir()用于返回指定的文件夹下包含的文件或文件夹名字的列表,这个列表按字母顺序排序。
Ⅳ python 怎么复制文件夹下部分文件
1、可以配置无密码访问或者用sshpass在shell中存密码
2、实例
ip.txt包含ip列表,每行一个ip
test.sh保护修改配置的命令或者直接修改好,复制到远程指定路径。
3、代码
#!/bin/sh
for
ip
in
`cat
ip.txt`;
do
echo
${ip};
scp
-
P22
test.sh
root...
Ⅵ python 怎么读取当前目录下指定文件
读文本文件
input = open('data', 'r')
#第二个参数默认为r
input = open('data')
读二进制文件
input = open('data', 'rb')
读取所有内容
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
读固定字节
file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )
读每行
list_of_all_the_lines = file_object.readlines( )
如果文件是文本文件,还可以直接遍历文件对象获取每行:
for line in file_object:
process line
Ⅶ python的运行文件在哪个文件夹
python的运行文件所在位置:
找到python的安装目录,在安装目录下面,有一个python.exe文件,这个就是python的运行文件
右键单击,选择发送到桌面快捷方式,这个快捷方式就是python的桌面图标了
示例如下:
更多Python知识,请关注:Python自学网!!
Ⅷ 如何用python遍历文件夹下的所有excel文件
大数据处理经常要用到一堆表格,然后需要把数据导入一个list中进行各种算法分析,简单讲一下自己的做法:
1.如何读取excel文件
网上的版本很多,在xlrd模块基础上,找到一些源码:
[python]view plain
importxdrlib,sys
importxlrd
defopen_excel(file="C:/Users/flyminer/Desktop/新建MicrosoftExcel工作表.xlsx"):
data=xlrd.open_workbook(file)
returndata
#根据索引获取Excel表格中的数据参数:file:Excel文件路径colnameindex:表头列名所在行的所以,by_index:表的索引
defexcel_table_byindex(file="C:/Users/flyminer/Desktop/新建MicrosoftExcel工作表.xlsx",colnameindex=0,by_index=0):
data=open_excel(file)
table=data.sheets()[by_index]
nrows=table.nrows#行数
ncols=table.ncols#列数
colnames=table.row_values(colnameindex)#某一行数据
list=[]
forrownuminrange(1,nrows):
row=table.row_values(rownum)
ifrow:
app={}
foriinrange(len(colnames)):
app[colnames[i]]=row[i]
list.append(app)
returnlist
#根据名称获取Excel表格中的数据参数:file:Excel文件路径colnameindex:表头列名所在行的所以,by_name:Sheet1名称
defexcel_table_byname(file="C:/Users/flyminer/Desktop/新建MicrosoftExcel工作表.xlsx",colnameindex=0,by_name=u'Sheet1'):
data=open_excel(file)
table=data.sheet_by_name(by_name)
nrows=table.nrows#行数
colnames=table.row_values(colnameindex)#某一行数据
list=[]
forrownuminrange(1,nrows):
row=table.row_values(rownum)
ifrow:
app={}
foriinrange(len(colnames)):
app[colnames[i]]=row[i]
list.append(app)
returnlist
defmain():
tables=excel_table_byindex()
forrowintables:
print(row)
tables=excel_table_byname()
forrowintables:
print(row)
if__name__=="__main__":
main()
- 最后一句是重点,所以这里也给代码人点个赞!
importos
importxlrd
importtest_wy
xpath="E:/唐伟捷/电力/电力系统总文件夹/舟山电力"
xtype="xlsx"
typedata=[]
name=[]
raw_data=[]
file_path=[]
defcollect_xls(list_collect,type1):
#取得列表中所有的type文件
foreach_elementinlist_collect:
ifisinstance(each_element,list):
collect_xls(each_element,type1)
elifeach_element.endswith(type1):
typedata.insert(0,each_element)
returntypedata
#读取所有文件夹中的xls文件
defread_xls(path,type2):
#遍历路径文件夹
forfileinos.walk(path):
foreach_listinfile[2]:
file_path=file[0]+"/"+each_list
#os.walk()函数返回三个参数:路径,子文件夹,路径下的文件,利用字符串拼接file[0]和file[2]得到文件的路径
name.insert(0,file_path)
all_xls=collect_xls(name,type2)
#遍历所有type文件路径并读取数据
forevey_nameinall_xls:
xls_data=xlrd.open_workbook(evey_name)
foreach_sheetinxls_data.sheets():
sheet_data=test_wy.excel_table_byname(evey_name,0,each_sheet.name)
#请参考读取excel文件的代码
raw_data.insert(0,sheet_data)
print(each_sheet.name,":Datahasbeendone.")
returnraw_data
a=read_xls(xpath,xtype)
print("Victory")
- 欢迎各种不一样的想法~~
最后一句让代码里的函数都可以被复用,简单地说:假设文件名是a,在程序中import a以后,就可以用a.excel_table_byname()和a.excel_table_byindex()这两个超级好用的函数了。
2.然后是遍历文件夹取得excel文件以及路径:,原创代码如下:
[python]view plain
Ⅸ python怎么查文件夹下所有文件
import os
import os.path
rootdir = “d:\data” # 指明被遍历的文件夹
for parent,dirnames,filenames in os.walk(rootdir): #三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字
for dirname in dirnames: #输出文件夹信息
print "parent is:" + parent
print "dirname is:" + dirname
for filename in filenames: #输出文件信息
print "parent is:" + parent
print "filename is:" + filename
print "the full name of the file is:" + os.path.join(parent,filename) #输出文件路径信息
Ⅹ python怎么获取路径下的所有文件夹名字
import os
os.chdir("D:\\python") #设置需要查看的目录
a = [x for x in os.listdir() if os.path.isdir(x)]
列表a里面就是当前目录下的文件夹名字