# python变量写文件

* 需要将subprocess处理结果写入文件

```python
f = open( 'file.py', 'w' )
f.write( 'dict = ' + repr(dict) + '\n' )
f.close()
```

```python
p = subprocess.Popen("cat /etc/redhat-release", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
res = p.communicate()[0]

f = open("/tmp/record.txt",'w')
f.write("%s %s" % (datetime.now(),res.strip()))
f.close()
```

* 清空文件内容

```python
open(filename, 'w').close()
```

## 参考

* [Write variable to file, including name](http://stackoverflow.com/questions/1900956/write-variable-to-file-including-name)
* [How to empty a file using Python](http://stackoverflow.com/questions/4914277/how-to-empty-a-file-using-python)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/python/startup/variable_write_file.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
