如果我在Python 2.7.5控制台中运行以下代码:
>>> import math
>>> math.radians(0.000001)
Run Code Online (Sandbox Code Playgroud)
我明白了
1.7453292519943295e-08
Run Code Online (Sandbox Code Playgroud)
但是,如果我将相同的代码放在一个文件中:
$ cat floatingtest.py
import math
print(math.radians(0.000001))
Run Code Online (Sandbox Code Playgroud)
运行它,我得到:
$ python.exe floatingtest.py
1.74532925199e-08
Run Code Online (Sandbox Code Playgroud)
为什么在脚本中运行代码与在控制台中运行代码时浮点精度的差异?
(Python 3.3似乎没有这个'问题'.两种方式都返回相同的高精度值.)
我想创建一个名为当前目录+文件夹+系统日期和时间的文件。我得到的输出为-
D:\Komal\MyPrograms\Pkg\stemwordwww.yahoo.com42015-03-18 16-31
Run Code Online (Sandbox Code Playgroud)
但我想存储名为
www.yahoo.com42015-03-18 16-31
Run Code Online (Sandbox Code Playgroud)
在文件夹中,stemword即要求输出为
D:\Komal\MyPrograms\Pkg\stemword\www.yahoo.com42015-03-18 16-31
Run Code Online (Sandbox Code Playgroud)
代码
def create_file(self,filename,folder):
print 'creating file....'
print 'file is---'
print filename
#Here our filename is url eg-www.amazon.in
dir = os.getcwd()
dir1 = os.path.join(dir,folder)
print 'directory---'
print dir1
date = datetime.datetime.now()
now = date.strftime("%Y-%m-%d %H-%M")
dirPath2 = os.path.join(dir1+filename)
dirPath = dirPath2.rstrip('\n')
filenameCreated = dirPath+now
print 'file is ---'
print filenameCreated
f = self.openfile(filenameCreated + '.txt', 'a')
f.close()
return filenameCreated
Run Code Online (Sandbox Code Playgroud)