写文件,但覆盖它,如果它存在 [英] Write to file, but overwrite it if it exists

查看:160
本文介绍了写文件,但覆盖它,如果它存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

echo "text" >> 'Users/Name/Desktop/TheAccount.txt'

我如何使它所以它创建文件,如果它不存在,但如果它已经存在,将其覆盖。现在,这个脚本只是追加。

How do I make it so it creates the file if it doesn't exist, but overwrites it if it already exists. Right now this script just appends.

推荐答案

如何* nix中管道的工作将有助于一点点的了解。

A little understanding of how *nix pipes work would help.

在短的>>管操作者将追加线到指定的文件的末尾,其中,作为单一的大于>将清空和覆盖该文件

In short the '>>' pipe operator will append lines to the end of the specified file, where-as the single greater than '>' will empty and overwrite the file.

echo "text" > 'Users/Name/Desktop/TheAccount.txt'

这篇关于写文件,但覆盖它,如果它存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆