【置顶】Python开发中常见问题

2018-01-02  本文已影响139人  keloli

本文长期更新
可以通过CTRL+F在页面内进行问题关键字搜索


参考资料:

问题汇总:


#文件上半部分定义函数
def A():
    #函数A的实现
def B():
    #函数B的实现

#文件下半部分调用函数并输出一些信息
A()
B()
print("this message should not be shown out of this file")

如果我们在outside.py文件中调用hub.py时,就会打印出this message should not be shown out of this file ,如果不希望别的文件调用hub.py时打印出上述信息,则可以将hub.py改成:

#文件上半部分定义函数
def A():
    #函数A的实现
def B():
    #函数B的实现

#文件下半部分调用函数并输出一些信息
#但是别的文件调用hub.py时不打印this message should not be shown out of this file
if __name__ == '__main__' #注意这句话
A()
B()
print("this message should not be shown out of this file")

这样,别的文件调用hub.py时就不会打印出this message should not be shown out of this file
原因就是:

  1. __name__ 是内置变量,用于表示当前模块的名字,同时还能反映一个包的结构。
  2. 如果一个模块被直接运行,则其没有包结构,其 __name__ 值为 __main__

Exception:
Traceback (most recent call last):
  File "D:\software\anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "D:\software\anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run
    wb.build(autobuilding=True)
  File "D:\software\anaconda3\lib\site-packages\pip\wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "D:\software\anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "D:\software\anaconda3\lib\site-packages\pip\req\req_set.py", line 554, in _prepare_file
    require_hashes
  File "D:\software\anaconda3\lib\site-packages\pip\req\req_install.py", line 278, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "D:\software\anaconda3\lib\site-packages\pip\index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "D:\software\anaconda3\lib\site-packages\pip\index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "D:\software\anaconda3\lib\site-packages\pip\index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "D:\software\anaconda3\lib\site-packages\pip\index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "D:\software\anaconda3\lib\site-packages\pip\index.py", line 811, in get_page
    inst = cls(resp.content, resp.url, resp.headers)
  File "D:\software\anaconda3\lib\site-packages\pip\index.py", line 731, in __init__
    namespaceHTMLElements=False,
TypeError: parse() got an unexpected keyword argument 'transport_encoding'

解决方法:
执行conda install pip





# coding=utf-8
# import xxxlib
"""
测试p当前文件的__doc__,输出显示本段文字
"""
print(__doc__) #输出“测试p当前文件的__doc__”

print(xxxlib.__doc__) #输出xxxlib这个库的文档注释
import os

def find_dir(dir_name="/data/测试图片/"):
    """
    将file_path下的所有jpg文件的绝对路径存储到list
    """
    print(find_dir.__doc__) #输出函数doc
    
    file_list=[]

    # 使用os.work(),path_name是当前目录,dir_list是当前目录下的所有目录名称,file_name是当前目录下的所有文件名称
    for path_name, dir_list, files_name in os.walk(dir_name):
        for file in files_name:
            if os.path.splitext(file)[1]=='.jpg':
                file_list.append(path_name+'/'+file)
            
        for dir_name in dir_list:
            find_dir(path_name+'/'+dir_name)

    return file_list #将所有文件的绝对路径存储在file_list[]中返回
def Trans2Unicode(str):
    c=bytearray() # 字节数组c
    for ch in str:
        c.append(ord(ch)) # 存成gbk
    print(type(c))
    return c.decode('gbk') # 将gbk转成unicode
上一篇 下一篇

猜你喜欢

热点阅读