[Tutor] 回复: help for the code debug

Sailormoon sccdzt at foxmail.com
Mon Oct 5 04:23:55 EDT 2020


Dear  Mr Alan :
I changed The data format of The excel ,as attached ,and I changed The  source path ,The directory as below screenshot ,The named file is in one of The below folder ,it can run ,but didn't copy The relative file to The nominated F disk ,Could You help  tell why is that .



#导入模块
#!/usr/bin/env python
# -*- coding: utf-8 -*-          #识别中文
import os
import shutil
import xlrd

#指定文件路径
path_file=r'G:\材料报验\WATER INJECTION 3720'

#便遍历文件夹中所有文件,此处以ls来表示,常用file
ls = os.listdir(path_file)
print(len(ls))

data = xlrd.open_workbook(r'C:\Users\Administrator\Desktop\searching.xls') # 打开xls文件
table = data.sheets()[0] # 打开第一张表
nrows = table.nrows      # 获取表的行数
#datalist用来存放数据
datalist_NAME=[]
#将table中第一列的数据读取并添加到data_list中
datalist_NAME.extend(table.col_values(0))

print(eval(datalist_NAME[1]) )#eval用于去除单引号,[1]指读取第二行元素

for num in range(1,nrows): #num从1到nrows遍历

   for i in ls:
      #利用模糊搜索find查找想要的文件
       if i.find(eval(datalist_NAME[num])+"_Boundary_Line")!=-1:
          shutil.copyfile(path_file+'\\'+i,"F:\\budyko_ds"+i)
          #shutil.move(path_file+'/'+i,"F:/budyko_ds/"+i)   #直接将文件移动过去
          print( datalist_NAME[num])

print( num)
print ("ALL DONE")




 




------------------ 原始邮件 ------------------
发件人:                                                                                                                        "Alan Gauld"                                                                                    <tutor at python.org>;
发送时间: 2020年10月5日(星期一) 下午4:04
收件人: "tutor"<tutor at python.org>;

主题: Re: [Tutor] help for the code debug



On 05/10/2020 07:28, Sailormoon wrote:
> Dear sir :
> When I run The code as below it showed error that&nbsp;SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers

Please always post the entire error message, never try to summarize it.
It contains lots of useful details.

However, in this case I suspect the problem is your use of eval()
It is a dangerous tool to use since it executes your data as if
it were code and can lead to serious security problems. It is rarely
necessary, and a conversion function is probably more appropriate
here.

> path_file=r'G:\材料报验\Before 2018.12.22'
> 
> ls = os.listdir(path_file)
> print(len(ls))
> 
> data = xlrd.open_workbook(r'C:\Users\Administrator\Desktop\searching.xls') # 打开xls文件
> table = data.sheets()[0] # 打开第一张表
> nrows = table.nrows      # 获取表的行数
> datalist_NAME=[]
> str(datalist_NAME.extend(table.col_values(0)))

In the above line str() does nothing since you don't
store the string value. Also the return value from
extend() is None so you are just converting None
to "None".

But the above would be more directly written as:

datalist_NAME = list(table.col_values(0))

> print(eval(datalist_NAME[1]) )

If the data contains invalid code Python will give
a SyntaxError, which may be what you are seeing.
But we can't tell because we can't see the data.
That's where the full error message would help.

> for num in range(1,nrows): #num从1到nrows遍历
>    for i in ls:
>        if i.find(eval(datalist_NAME[num])+"_Boundary_Line")!=-1:

Again this use of eval is potentially dangerous. I suspect
a simple str() call would suffice? But without seeing what
kind of data you have we can't be sure.

Similarly the find() call could be clarified by using 'in' instead:

for file_name in ls:
    if str(datalist_NAME(num)+"Boundary_Line" not in file_name:

>           shutil.copyfile(path_file+'/'+i,"F:/budyko_ds/"+i)
>           #shutil.move(path_file+'/'+i,"F:/budyko_ds/"+i)   #直接将文件移动过去
>           print( datalist_NAME[num])

Notice you are printing the raw data here but you
have eval()'d it earlier.
> print( num)
> print ("ALL DONE")

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list