如果目录不存在,我在这里有一段代码会中断:
System.IO.File.WriteAllText(filePath, content);
Run Code Online (Sandbox Code Playgroud)
在一行(或几行)中,是否可以检查导致新文件的目录是否不存在,如果不存在,是否可以在创建新文件之前创建它?
我正在使用.NET 3.5.
我有一个C++/Obj-C背景,我只是发现了Python(已经写了大约一个小时).我正在编写一个脚本,以递归方式读取文件夹结构中的文本文件的内容.
我遇到的问题是我编写的代码只适用于一个文件夹.我可以在代码中看到原因(参见参考资料#hardcoded path),我只是不知道如何继续使用Python,因为我的经验只是全新的.
Python代码:
import os
import sys
rootdir = sys.argv[1]
for root, subFolders, files in os.walk(rootdir):
for folder in subFolders:
outfileName = rootdir + "/" + folder + "/py-outfile.txt" # hardcoded path
folderOut = open( outfileName, 'w' )
print "outfileName is " + outfileName
for file in files:
filePath = rootdir + '/' + file
f = open( filePath, 'r' )
toWrite = f.read()
print "Writing '" + toWrite + "' to" + filePath
folderOut.write( toWrite ) …Run Code Online (Sandbox Code Playgroud) 我想删除ABC目录中的所有文件.
当我尝试使用FileUtils.deleteDirectory(new File("C:/test/ABC/"));它时也删除文件夹ABC.
有一个单线解决方案,我可以删除目录内的文件,但不删除目录?
我正在研究如何在Python中进行文件输入和输出.我编写了以下代码来读取文件中的名称列表(每行一个)到另一个文件,同时根据文件中的名称检查名称,并将文本附加到文件中的出现位置.代码有效.可以做得更好吗?
我想对with open(...输入和输出文件使用该语句,但无法看到它们在同一块中的含义,这意味着我需要将名称存储在临时位置.
def filter(txt, oldfile, newfile):
'''\
Read a list of names from a file line by line into an output file.
If a line begins with a particular name, insert a string of text
after the name before appending the line to the output file.
'''
outfile = open(newfile, 'w')
with open(oldfile, 'r', encoding='utf-8') as infile:
for line in infile:
if line.startswith(txt):
line = line[0:len(txt)] + ' - Truly a great person!\n'
outfile.write(line)
outfile.close()
return # …Run Code Online (Sandbox Code Playgroud) 我想通过获取从EditText输入的文本将文件保存到内部存储.然后我希望同一个文件以String形式返回输入的文本并将其保存到另一个稍后要使用的String中.
这是代码:
package com.omm.easybalancerecharge;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText num = (EditText) findViewById(R.id.sNum);
Button ch = (Button) findViewById(R.id.rButton);
TelephonyManager operator = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String opname = operator.getNetworkOperatorName();
TextView status = (TextView) findViewById(R.id.setStatus);
final EditText ID = (EditText) findViewById(R.id.IQID);
Button save = (Button) findViewById(R.id.sButton);
final String …Run Code Online (Sandbox Code Playgroud) 有没有办法打开文件进行读写?
作为一种解决方法,我打开文件进行写入,关闭它,然后再次打开它进行读取.但是,有没有办法打开一个文件都阅读和写作?
有没有办法以一种方式使用FileOutputStream,如果文件(String filename)不存在,那么它会创建它吗?
FileOutputStream oFile = new FileOutputStream("score.txt", false);
Run Code Online (Sandbox Code Playgroud) Ruby File.open将模式和选项作为参数.我在哪里可以找到完整的模式和选项列表?
我正在为Web应用程序编写一个日志文件查看器,为此我想通过日志文件的行分页.文件中的项目是基于行的,底部是最新项目.
所以我需要一种tail()方法,可以n从底部读取行并支持偏移量.我想出的是这样的:
def tail(f, n, offset=0):
"""Reads a n lines from f with an offset of offset lines."""
avg_line_length = 74
to_read = n + offset
while 1:
try:
f.seek(-(avg_line_length * to_read), 2)
except IOError:
# woops. apparently file is smaller than what we want
# to step back, go to the beginning instead
f.seek(0)
pos = f.tell()
lines = f.read().splitlines()
if len(lines) >= to_read or pos == 0:
return lines[-to_read:offset and -offset or None]
avg_line_length …Run Code Online (Sandbox Code Playgroud)