Copy file if it doesn't already exist

This question already has an answer here: How to check whether a file exists? 39 answers In Python, it can often be seen that you will hit errors, known as exceptions , when running code. For this reason, the try/catch has been deployed. Here is a snippet of code I use in my day-to-day that clears files from a directory, or skips if they do not exist. def DeleteFile(Path_): """Delete

如果文件不存在,请复制文件

这个问题在这里已经有了答案: 如何检查文件是否存在? 39个答案 在Python中,通常可以看到,运行代码时会遇到称为exceptions错误。 出于这个原因, try/catch已经被部署。 以下是我在日常使用的代码片段,用于清除目录中的文件 ,或者在不存在的情况下跳过。 def DeleteFile(Path_): """Deletes saved project AND its corresponding "files" folder.""" try: #deletes the folder os.remove(Path_)

Python if statement error handling

Possible Duplicate: How do I check if a file exists using Python? I am kind of a beginner learning python, but I got this bit of code that I want to handle in a way if someone was too enter an invalid path that does not exists like Z: instead of C: I want it to raise some sort of error, now I know how to go about doing it, I just do not know how to check if it will be invalid? like how can I

Python if语句错误处理

可能重复: 如何使用Python检查文件是否存在? 我是一个初学者学Python的人,但是我得到了一些我想以某种方式处理的代码,如果有人输入了一个无效的路径,而这个路径并不像Z:而是C:我想让它提高某种错误,现在我知道如何去做,我只是不知道如何检查它是否会失效? 比如我怎样才能让我的代码检查,如果该驱动器或路径存在于这台PC之前,它创建的路径,有没有办法做到这一点在Python中? website_path = input("nEnter the

Pythonic way to check if a file exists?

This question already has an answer here: How to check whether a file exists? 39 answers To check if a path is an existing file: os.path.isfile(path) Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path. Instead of os.path.isfile , suggested by others, I suggest using os.path.exists , which checks for

Pythonic的方式来检查文件是否存在?

这个问题在这里已经有了答案: 如何检查文件是否存在? 39个答案 检查路径是否是现有文件: os.path.isfile(path) 如果路径是现有的常规文件,则返回True 。 这遵循符号链接,所以islink()和isfile()对于相同的路径都可以为true。 取而代之的os.path.isfile ,建议由别人,我建议使用os.path.exists ,检查与该名什么,不只是它是否是一个普通的文件。 从而: if not os.path.exists(filename): file(filename

atribute of object in python?

是否正确更改对象的__name__属性的值,如下例所示: >>> >>> def f(): pass ... >>> f.__name__ 'f' >>> b = f >>> b.__name__ 'f' >>> b.__name__ = 'b' >>> b <function b at 0x0000000002379278> >>> b.__name__ 'b' >>> Changing a function's name doesn't make the new name callable: >>> def f(): print 'cal

Python中对象的属性?

是否正确更改对象的__name__属性的值,如下例所示: >>> >>> def f(): pass ... >>> f.__name__ 'f' >>> b = f >>> b.__name__ 'f' >>> b.__name__ = 'b' >>> b <function b at 0x0000000002379278> >>> b.__name__ 'b' >>> 更改函数的名称不会使新名称可调用: >>> def f(): print 'called %s'%(f.__name__) ... >>>

generate video pixel by pixel, programatically

I'd like to generate an animation pixel by pixel programmatically. Preferably in Hi-Def, in Python or in Ruby. I thought about using PIL to make each frame and then convert the frames into video. Is there a better way to do this? EDIT: Clarification, this is 2D and I need the pixels to be precise. EDITEDIT: Something like this: frame = Frame() frame.draw(0, 0, 'red') frame.draw(0, 1

以编程方式逐像素生成视频

我想以编程方式按像素生成动画像素。 最好在Hi-Def,Python或Ruby中。 我想过使用PIL制作每个帧,然后将帧转换为视频。 有一个更好的方法吗? 编辑:澄清,这是2D,我需要的像素是精确的。 EDITEDIT: 像这样的东西: frame = Frame() frame.draw(0, 0, 'red') frame.draw(0, 1, 'blue') ... frame = Frame() ... 会很棒。 结帐VPython。 它非常容易,可以做你正在寻找的东西。 rcairo - 红宝石绑定开罗图像库

Converting a list of points to a numpy 2D array

I'm using genfromtxt to import essentially a 2D array that has all its values listed in a text file of the form (x's and y's are integers): x1 y1 z1 x2 y2 z2 : : : I'm using the for loop below but I'm pretty sure there must be a one line way to do it. What would be a more efficient way to do this conversion? raw = genfromtxt(file,skip_header = 6)

将点列表转换为numpy 2D数组

我使用genfromtxt来导入基本上是一个二维数组,它的所有值列在表单的文本文件中(x和y都是整数): x1 y1 z1 x2 y2 z2 : : : 我在下面使用for循环,但我很确定必须有一条线路来完成它。 什么是更有效的方式来做这种转换? raw = genfromtxt(file,skip_header = 6) xrange = ( raw[:,0].min() , raw[:,0].max() ) yrange = ( raw[:,1].min() , raw[:,1].max() ) Z = zeros(( xrange[1] - xrange[0

Why does `type(myField)` return `<type 'instance'>` and not `<type 'Field'>`?

I am confronted to a python problem. I want to use type() to find out what type of variable I am using. The code looks similar to this one: class Foo(): array=[ myField(23),myField(42),myField("foo"), myField("bar")] def returnArr(self): for i in self.array: print type(i) if __name__ == "__main__": a=Foo() a.returnArr() Edit: myField() is a clas

为什么`type(myField)`返回`<type'instance'>`而不是'<type'Field'>`?

我遇到了python问题。 我想用type()来找出我正在使用的变量类型。 代码看起来类似于这个: class Foo(): array=[ myField(23),myField(42),myField("foo"), myField("bar")] def returnArr(self): for i in self.array: print type(i) if __name__ == "__main__": a=Foo() a.returnArr() 编辑:myField()是我定义的类。 当我问的类型()我得到: <type 'instan

Parse date and time from string with time zone using Arrow

I have import arrow s = '2015/12/1 19:00:00' tz = 'Asia/Hong_Kong' How can I parse this with Arrow such that I get an Arrow object with the time zone tz ? The following defaults to UTC time. In [30]: arrow.get(s, 'YYYY/M/D HH:mm:ss') Out[30]: <Arrow [2015-12-01T19:00:00+00:00]> I know the .to function but that converts a time zone and but doesn't allow me to change to time zone.

使用箭头解析带时区的字符串的日期和时间

我有 import arrow s = '2015/12/1 19:00:00' tz = 'Asia/Hong_Kong' 我怎样才能解析这个箭头,使我得到一个箭头对象的时区tz ? 以下内容默认为UTC时间。 In [30]: arrow.get(s, 'YYYY/M/D HH:mm:ss') Out[30]: <Arrow [2015-12-01T19:00:00+00:00]> 我知道.to功能,但它转换时区,但不允许我更改为时区。 尝试这个: arrow.get(s, 'YYYY/M/D HH:mm:ss').replace(tzinfo=dateutil.tz.gettz(tz)) 我还没有资格添加

How do I create a constant in Python?

Is there a way to declare a constant in Python? In Java we can create constant values in this manner: public static final String CONST_NAME = "Name"; What is the equivalent of the above Java constant declaration in Python? No there is not. You cannot declare a variable or value as constant in Python. Just don't change it. If you are in a class, the equivalent would be: class Foo(obj

如何在Python中创建常量?

有没有办法在Python中声明一个常量? 在Java中,我们可以用这种方式创建常量值: public static final String CONST_NAME = "Name"; Python中的上述Java常量声明等价于什么? 不,那里没有。 您不能在Python中将变量或值声明为常量。 只是不要改变它。 如果你在一个班级,相当于: class Foo(object): CONST_NAME = "Name" 如果没有,这是公正的 CONST_NAME = "Name" 但你可能想看看由Alex Martelli编写的Pytho

Use of isinstance() can overwrite type

This question already has an answer here: What are metaclasses in Python? 14 answers I think your confusion lies in the fact that type(dict) != dict . Lets discard your example entirely except for the last two lines, which I will use interactive python to present. >>> type(dict) <type 'type'> >>> type(dict()) <type 'dict'> This is because dict is not a dictio

使用isinstance()可以覆盖类型

这个问题在这里已经有了答案: Python中的元类是什么? 14个答案 我认为你的困惑在于type(dict) != dict的事实。 除了最后两行,我将使用交互式python来呈现,让我们完全抛弃你的例子。 >>> type(dict) <type 'type'> >>> type(dict()) <type 'dict'> 这是因为dict不是字典,而是字典的类型。 dict()或{} (或{1:2, ...} )是词典的实例。 这些实例有类型的dict ,并满足isinstance(___