奇怪的 write()/文件行为

huangapple go评论76阅读模式
英文:

Strange write()/File behaviour

问题

lines = ['错误标题:用户 ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' 数据框为空']
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
    file.write('\n'.join(lines))
    file.write('\n')
    file.close()
def 写入日志(lines):
    with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
        file.write('\n'.join(lines))
        file.write('\n')
        file.close()
写入日志(['错误标题:用户 ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' 数据框为空'])
英文:

Hi I have a long python application, and within it's workflow I am trying to write snippets of strings to an error log.

I was using:

lines = ['ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user']
with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
    file.write('\n'.join(lines))
    file.write('\n')
    file.close()

This worked and the result was a single line of text in a text document expressing a specific error I wanted to record.

The issue came when I realised I was duplicating the with open call throughout my code. So I attempted to convert the write open call to a custom function like this:

def write_to_log(lines):

    with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
        file.write('\n'.join(lines))
        file.write('\n')
        file.close()

Then I call the write to log function each time I need to pass it the list to write like this:

write_to_log([ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user])

Now however, I get weird output within the text file. instead of readable text I get:

奇怪的 write()/文件行为

I then commented out the change and reverted back to a write file call per error text call. but I seem to be getting the error no matter what now.

Scope:

I am writing and executing the code via VSCode on a mac. the text file is going to a file location on a network NAS, and I have tried to open the file on various machines and with various text editors (as I thought it may be a NotePad issue on my windows machine), but the bad characters remain.

I am clearly doing something stupid. all assistance greatly appreciated.

Kind regards

Duncan

答案1

得分: 1

  1. 你在使用上下文管理器时调用了file.close(),但你不需要这样做,因为文件会自动关闭。
  2. 尝试指定编码方式。
def write_to_log(lines):
    with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a', encoding='utf-8') as file:
        file.write('\n'.join(lines) + '\n')
英文:
  1. Youre calling file.close() while using context manager, but you dont need to because file will be closed automatically
  2. Try to specify the encoding
def write_to_log(lines):
    with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a', encoding='utf-8') as file:
        file.write('\n'.join(lines) + '\n')


</details>



# 答案2
**得分**: 0

```python
def write_to_log(lines):

    with open(ERROR_LOG_PATH + ERROR_LOG_NAME, 'a') as file:
        file.write('\n'.join(lines))
        file.write('\n')
        file.close()

user_id = 1
firstName ='hello'
lastName = 'goodbye'

ERROR_LOG_PATH = 'rest'
ERROR_LOG_NAME ='test'
lines = ['ERROR TITLE: user ' + str(user_id) + ' ' + firstName + ' ' + lastName + ' dataframe is empty for this user']

write_to_log(lines) # 在此处传递了行
英文:

Your code seems to be working if this is how you intended

def write_to_log(lines):

    with open(ERROR_LOG_PATH + ERROR_LOG_NAME, &#39;a&#39;) as file:
        file.write(&#39;\n&#39;.join(lines))
        file.write(&#39;\n&#39;)
        file.close()

user_id = 1
firstName =&#39;hello&#39;
lastName = &#39;goodbye&#39;

ERROR_LOG_PATH = &#39;rest&#39;
ERROR_LOG_NAME =&#39;test&#39;
lines = [&#39;ERROR TITLE: user &#39; + str(user_id) + &#39; &#39; + firstName + &#39; &#39; + lastName + &#39; dataframe is empty for this user&#39;]

write_to_log(lines) //passed lines here

OUTPUT

ERROR TITLE: user 1 hello goodbye dataframe is empty for this user

huangapple
  • 本文由 发表于 2023年7月4日 22:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定