草庐IT

path_str

全部标签

python - 如何为类定义 __str__ 方法?

在Python中,object类充当所有(新式)类的根父类(superclass)。至少默认情况下,将str和repr应用于object的任何子类的“类实例”会产生相同的结果:>>>classspam(object):pass...>>>str(spam)"">>>str(spam)==repr(spam)我想定义一个object的子类,比如fancyobject,它在各方面都与object相同,除了应用str和repr到fancyobject本身会产生不同的输出:>>>classham(fancyobject):pass...>>>str(ham)'ham'>>>repr(ham)"

python - 如何转换 os.path.getctime()?

如何将os.path.getctime()转换为正确的时间?我的源代码是:importosprint("MyPath:"+os.getcwd())print(os.listdir("."))print("Root/:",os.listdir("/"))foritemsinos.listdir("."):ifos.path.isdir(items):print(items+""+"IsaDirectory")print("---Information:")print("*FullName:",os.path.dirname(items))print("*CreatedTime:",os.

python - 如何转换 os.path.getctime()?

如何将os.path.getctime()转换为正确的时间?我的源代码是:importosprint("MyPath:"+os.getcwd())print(os.listdir("."))print("Root/:",os.listdir("/"))foritemsinos.listdir("."):ifos.path.isdir(items):print(items+""+"IsaDirectory")print("---Information:")print("*FullName:",os.path.dirname(items))print("*CreatedTime:",os.

python - 无法连接 'str' 和 'float' 对象?

这个问题在这里已经有了答案:HowcanIconcatenatestrandintobjects?(1个回答)关闭2个月前。我们的几何老师给了我们一个作业,要求我们创建一个玩具在现实生活中使用几何的例子,所以我认为编写一个程序来计算填充一个游泳池需要多少加仑的水会很酷有一定的形状,有一定的尺寸。这是目前为止的程序:importeasyguieasygui.msgbox("Thisprogramwillhelpdeterminehowmanygallonswillbeneededtofillupapoolbasedoffofthedimensionsgiven.")pool=easygu

python - 无法连接 'str' 和 'float' 对象?

这个问题在这里已经有了答案:HowcanIconcatenatestrandintobjects?(1个回答)关闭2个月前。我们的几何老师给了我们一个作业,要求我们创建一个玩具在现实生活中使用几何的例子,所以我认为编写一个程序来计算填充一个游泳池需要多少加仑的水会很酷有一定的形状,有一定的尺寸。这是目前为止的程序:importeasyguieasygui.msgbox("Thisprogramwillhelpdeterminehowmanygallonswillbeneededtofillupapoolbasedoffofthedimensionsgiven.")pool=easygu

Python 3.5+ : How to dynamically import a module given the full file path (in the presence of implicit sibling imports)?

问题标准库明确记录howtoimportsourcefilesdirectly(给定源文件的绝对文件路径),但如果源文件使用下面示例中描述的隐式同级导入,则此方法不起作用。如果存在隐式同级导入,该示例如何适应工作?我已经checkoutthis和thisotherStackoverflow有关该主题的问题,但它们没有解决手动导入的文件内的隐式同级导入。设置/示例这是一个说明性示例目录结构:root/-directory/-app.py-folder/-implicit_sibling_import.py-lib.pyapp.py:importosimportimportlib.util

Python 3.5+ : How to dynamically import a module given the full file path (in the presence of implicit sibling imports)?

问题标准库明确记录howtoimportsourcefilesdirectly(给定源文件的绝对文件路径),但如果源文件使用下面示例中描述的隐式同级导入,则此方法不起作用。如果存在隐式同级导入,该示例如何适应工作?我已经checkoutthis和thisotherStackoverflow有关该主题的问题,但它们没有解决手动导入的文件内的隐式同级导入。设置/示例这是一个说明性示例目录结构:root/-directory/-app.py-folder/-implicit_sibling_import.py-lib.pyapp.py:importosimportimportlib.util

python - 如何从 pathlib.path 获取给定文件所在的文件夹名称?

有没有类似于os.path.dirname(path)的东西,但是在pathlib中? 最佳答案 看起来有一个parents元素包含给定路径的所有父目录。例如,如果你开始:>>>importpathlib>>>p=pathlib.Path('/path/to/my/file')那么p.parents[0]就是包含file的目录:>>>p.parents[0]PosixPath('/path/to/my')...和​​p.parents[1]将是下一个目录:>>>p.parents[1]PosixPath('/path/to')等等p

python - 如何从 pathlib.path 获取给定文件所在的文件夹名称?

有没有类似于os.path.dirname(path)的东西,但是在pathlib中? 最佳答案 看起来有一个parents元素包含给定路径的所有父目录。例如,如果你开始:>>>importpathlib>>>p=pathlib.Path('/path/to/my/file')那么p.parents[0]就是包含file的目录:>>>p.parents[0]PosixPath('/path/to/my')...和​​p.parents[1]将是下一个目录:>>>p.parents[1]PosixPath('/path/to')等等p

python - 在 Python 中打印对象列表时如何应用 __str__ 函数

这个问题在这里已经有了答案:Howtoprintinstancesofaclassusingprint()?(12个回答)关闭3个月前。这个交互式python控制台片段将说明一切:>>>classTest:...def__str__(self):...return'asd'...>>>t=Test()>>>print(t)asd>>>l=[Test(),Test(),Test()]>>>print(l)[__main__.Testinstanceat0x00CBC1E8,__main__.Testinstanceat0x00CBC260,__main__.Testinstanceat0