尝试写入文件并记录普通文本时出现错误。这是我的记录器输出:
Bad measurement: 40.74478609626E-9,81.48235294118E-9,163.01491596639E-9,244.51470588235E-9,326.05084670232E-9,407.55387700535E-9,489.09705882353E-9,570.63334914611E-9,652.15799632353E-9,733.68823529412E-9,815.19491596639E-9,1.30423299632E-6,2.60858733660E-6,6.11542963458E-6,13.04562372549E-6,26.09313266488E-6,39.13227058824E-6,52.17394175579E-6,78.25093569519E-6,163.01165588235E-6,305.64046063025E-6,611.30138063025E-6,815.07555294118E-6,1.01882401751E-3,1.22261084479E-3,2.44530079118E-3,9.9100E+037
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.5\helpers\pydev\pydev_run_in_console.py", line 53, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:/Projects/main.py", line 209, in <module>
path = "data.txt"
File "C:/Projects/main.py", line 177, in run_period
logger.info("Error: {}".format(repr(e)))
IOError: [Errno 22] Invalid argument
上下文:我正在从示波器读取测量值并将其保存到文件中。有时,作用域中的数据将被破坏,因此我将写操作包装在try-catch中:
try:
f.write(",".join(acq) + "\n")
cnt += 1
except Exception as e:
logger.info("Bad measurement: {}".format(",".join(acq)))
logger.info("Error: {}".format(repr(e)))
logger.info("The measurement couldn't be written to the file")
是的,我知道不好的做法是不使用特定的异常,但是我只是想获取数据并退出。
记录器已分配给
logging.basicConfig(format='%(message)s', level=logging.INFO)
logger = logging.getLogger("runScript")
我认为这可能与the Windows OS buffer limit有关,但是我的文件只有几百千字节。
引发错误的函数是PyCharm的一部分。功能如下:
def run_file(file, globals=None, locals=None, is_module=False):
module_name = None
entry_point_fn = None
if is_module:
file, _, entry_point_fn = file.partition(':')
module_name = file
filename = get_fullname(file)
if filename is None:
sys.stderr.write("No module named %s\n" % file)
return
else:
file = filename
if os.path.isdir(file):
new_target = os.path.join(file, '__main__.py')
if os.path.isfile(new_target):
file = new_target
if globals is None:
m = save_main_module(file, 'pydev_run_in_console')
globals = m.__dict__
try:
globals['__builtins__'] = __builtins__
except NameError:
pass # Not there on Jython...
if locals is None:
locals = globals
if not is_module:
sys.path.insert(0, os.path.split(file)[0])
print('Running %s' % file)
try:
if not is_module:
pydev_imports.execfile(file, globals, locals) # execute the script
else:
# treat ':' as a seperator between module and entry point function
# if there is no entry point we run we same as with -m switch. Otherwise we perform
# an import and execute the entry point
if entry_point_fn:
mod = __import__(module_name, level=0, fromlist=[entry_point_fn], globals=globals, locals=locals)
func = getattr(mod, entry_point_fn)
func()
else:
# Run with the -m switch
import runpy
if hasattr(runpy, '_run_module_as_main'):
runpy._run_module_as_main(module_name)
else:
runpy.run_module(module_name)
except:
traceback.print_exc()
return globals