How do I change directory (cd) in Python?

cd as in the shell command to change the working directory. How do I change the current working directory in Python? You can change the working directory with: import os os.chdir(path) There are two best practices to follow when using this method: Catch the exception (WindowsError, OSError) on invalid path. If the exception is thrown, do not perform any recursive operations, especially

如何在Python中更改目录(cd)?

cd在shell命令中更改工作目录。 如何在Python中更改当前工作目录? 您可以通过以下方式更改工作目录: import os os.chdir(path) 使用此方法时有两种最佳做法: 在无效路径上捕获异常(WindowsError,OSError)。 如果引发异常,请不要执行任何递归操作,特别是破坏性操作。 他们将在旧的道路上运作,而不是新的道路。 完成后返回到旧的目录。 这可以通过将你的chdir调用包装在上下文管理器中以异常安全的方式完成

catching an IOError in python

I wrote a method that does some stuff and catches bad filenames. what should happen is if the path doesn't exist, it throws an IOError. however, it thinks my exception handling is bad syntax... why?? def whatever(): try: # do stuff # and more stuff except IOError: # do this pass whatever() but before it even gets to calling whatever() , it prints t

在Python中捕获IOError

我写了一个方法来完成一些东西并捕获错误的文件名。 如果路径不存在,会发生什么情况,它将引发IOError。 然而,它认为我的异常处理是不好的语法...为什么? def whatever(): try: # do stuff # and more stuff except IOError: # do this pass whatever() 但在它调用whatever() ,它会打印以下内容: Traceback (most recent call last): File "", line 1, in File "getqui

Python IOError exception when creating a long file

I get an IOError shown below when trying to open a new file using "open (fname, 'w+')". The complete error message is below. The file does not exist, but I verified using "os.access(dir_name, os.W_OK)" and "os.path.exists (dir_name)" that the parent directory for the file does exist. I am wondering if the file name is just too long for Windows, or if I a

创建长文件时出现Python IOError异常

尝试使用“open(fname,'w +')”打开新文件时,出现下面显示的IOError。 完整的错误信息如下。 该文件不存在,但我使用“os.access(dir_name,os.W_OK)”和“os.path.exists(dir_name)”验证文件的父目录是否存在。 我想知道如果文件名对于Windows来说太长了,或者我做错了什么。 任何提示将不胜感激。 非常感谢你。 错误信息: IOError:[Errno 2] No such file or directory:'C: Documents and Setti

Using module 'subprocess' with timeout

Here's the Python code to run an arbitrary command returning its stdout data, or raise an exception on non-zero exit codes: proc = subprocess.Popen( cmd, stderr=subprocess.STDOUT, # Merge stdout and stderr stdout=subprocess.PIPE, shell=True) communicate is used to wait for the process to exit: stdoutdata, stderrdata = proc.communicate() The subprocess module does not supp

超时使用模块“subprocess”

以下是运行任意命令返回stdout数据的Python代码,或在非零退出代码上引发异常: proc = subprocess.Popen( cmd, stderr=subprocess.STDOUT, # Merge stdout and stderr stdout=subprocess.PIPE, shell=True) communicate用于等待进程退出: stdoutdata, stderrdata = proc.communicate() subprocess进程模块不支持超时 - 杀死运行超过X秒的进程的能力 - 因此, communicate可能需要永远运行。 在Windows

How to read all message from queue using stomp library in Python?

How can I read all messages from stomp queue in Python? I wrote such code but it reads only one message and exists - how to force read all messages. # coding=utf-8 import stomp import logging from medptr.farm.farm import FarmSettings import platform import os if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) class Connect

如何使用Python中的stomp库读取队列中的所有消息?

我怎样才能从Python中的stomp队列读取所有消息? 我写了这样的代码,但它只读取一条消息并存在 - 如何强制读取所有消息。 # coding=utf-8 import stomp import logging from medptr.farm.farm import FarmSettings import platform import os if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) class ConnectionListener(stomp.ConnectionListene

Lazy logger message string evaluation

I'm using standard python logging module in my python application: import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger("log") while True: logger.debug('Stupid log message " + ' '.join([str(i) for i in range(20)]) ) # Do something The issue is that although debug level is not enable, that stupid log message is evaluated on each loop iteration, which harms p

懒惰的记录器消息字符串评估

我在我的python应用程序中使用标准的python日志记录模块: import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger("log") while True: logger.debug('Stupid log message " + ' '.join([str(i) for i in range(20)]) ) # Do something 问题是,虽然调试级别未启用,但每次循环迭代都会评估该愚蠢的日志消息,这会严重影响性能。 有没有解决方案? 在C ++中,我们有提供如下宏的log4cxx

python django logging problem

I use logging settings as below in the settings.py file: logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT); handler = logging.handlers.RotatingFileHandler( LOG_FILE_PATH, 'a', LOG_FILE_SIZE,LOG_FILE_NUM ); formatter = logging.Formatter ( LOG_FORMAT ); handler.setFormatter(formatter); logging.getLogger().addHandler(handler) and i use mod_python with apache2. the problem

python django日志记录问题

我在settings.py文件中使用如下的日志记录设置: logging.basicConfig(level = LOG_LEVEL,format = LOG_FORMAT); handler = logging.handlers.RotatingFileHandler(LOG_FILE_PATH,'a',LOG_FILE_SIZE,LOG_FILE_NUM); formatter = logging.Formatter(LOG_FORMAT); handler.setFormatter(格式化器); logging.getLogger()。addHandler操作(处理) 我使用mod_python和apache2。 问题是:当日志

Class for file creation and directory validation

After reading some texts regarding creation of files under python, i decided to create this class which creates a new file on a directory, and creating a backup on the other directory if the file already exists (and if it's older than x hours ) The main reason i opened this question is to know if this is a correct way to write a class using try/except correctly, because actually i'm get

文件创建和目录验证类

在阅读了关于在python下创建文件的一些文本之后,我决定创建这个类,它在一个目录上创建一个新文件,并且在该文件已经存在的情况下在另一个目录上创建一个备份(并且如果它早于x小时) 我打开这个问题的主要原因是要知道,如果这是一个正确的方式来写一个类使用尝试/除非正确,因为实际上我对使用尝试/除了而不是/ elses的偏好有点困惑。 贝娄,工作的例子: import os import datetime class CreateXML(): def __init

Is it a good practice to use try

From time to time in Python, I see the block: try: try_this(whatever) except SomeException as exception: #Handle exception else: return something What is the reason for the try-except-else to exist? I do not like that kind of programming, as it is using exceptions to perform flow control. However, if it is included in the language, there must be a good reason for it, isn't it?

使用try是一个好习惯

不时在Python中,我看到该块: try: try_this(whatever) except SomeException as exception: #Handle exception else: return something try-except-else存在的原因是什么? 我不喜欢那种编程,因为它使用异常来执行流量控制。 但是,如果它被包含在语言中,那么一定有充分的理由,不是吗? 我的理解是,异常不是错误 ,并且它们只能用于特殊条件(例如,我尝试将文件写入磁盘并且没有更多空间,或者我没有权限

What is Ruby's equivalent to Python's multiprocessing module?

To get real concurrency in Ruby or Python, I need to create new processes. Python makes this pretty straightforward using the multiprocessing module, which abstracts away all the fork / wait goodness and lets me concentrate on my code. Does Ruby have something similar? Right now I am calling Process.fork and Process.wait to get my concurrency, and I want a cleaner solution. I have used https

什么是Ruby的等同于Python的多处理模块?

为了在Ruby或Python中获得真正的并发性,我需要创建新的进程。 Python使用multiprocessing模块使得这个过程非常简单,它可以抽象出所有的分支/等待善意,并让我专注于我的代码。 Ruby有类似的东西吗? 现在我正在调用Process.fork和Process.wait来获得我的并发性,并且我想要一个更干净的解决方案。 我使用了https://github.com/grosser/parallel,并且喜欢它很多。 默认情况下,它将#map或#each跨系统中的所有内核。 在