[Tutor] help for the code debug

Alan Gauld alan.gauld at yahoo.co.uk
Mon Oct 5 04:04:35 EDT 2020


On 05/10/2020 07:28, Sailormoon wrote:
> Dear sir :
> When I run The code as below it showed error that 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




More information about the Tutor mailing list