My instructor provided the following code, but it is not working on OS X when run from command line.
file_name = 'data/' + raw_input('Enter the name of your file: ') + '.txt'
fout = open(file_name, 'w')
Error message:
Traceback (most recent call last):
File "write_a_poem_to_file.py", line 12, in <module>
fout = open(file_name, 'w')
IOError: [Errno 2] No such file or directory: 'data/poem1.txt'
I have been writing Python since before I got to the class and having done a little research, it think you need to import the os module to create a directory.
Then you can specify that you want to create a file in that directory.
I believe you might also have to switch into that directory before accessing files.
I may be wrong, and I am wondering if I am missing another issue.
As stated by #Morgan Thrapp in the comments, the open() method won't create a folder for you.
If the folder /data/ already exists, it should work fine.
Otherwise you'll have to check if the folder exists, if not, then create the folder.
import os
if not os.path.exists(directory):
os.makedirs(directory)
So.. your code:
file_name = 'data/' + raw_input('Enter the name of your file: ') + '.txt'
fout = open(file_name, 'w')
Became something like this:
import os
folder = 'data/'
if not os.path.exists(folder):
os.makedirs(folder)
filename = raw_input('Enter the name of your file: ')
file_path = folder + filename + '.txt'
fout = open(file_path, 'w')
Check if folder "data" does not exist. If does not exist you have to create it:
import os
file_name = 'data/' + raw_input('Enter the name of your file: ') + '.txt'
if not os.path.exists('data'):
os.makedirs('data')
fout = open(file_name, 'w')
Related
I wanted to supply python with a windows 'data path' that could be used to set up input processing. I googled this with no luck, and now figure I am on my own.
There appears to be many ways of reading in a file with python, and after some frustration with "\" and "/" and windows path names I found a way to get my data path set up. It is not a general approach but should serve me well.
Related Questions: Is this code ugly? Is this a nonstandard method? Are there elegant features in 3.6 that should be used?
### Title: Process an input file using a 'data path' for a user on windows
import sys
import os
print("OK, starting program...")
file_processed = False
for path, dirs, files in os.walk("/Users/Mike"):
if file_processed: break
for file in files:
if file_processed: break
if file == 'seriousdata.txt':
my_path = os.path.abspath(path)
my_dsn = os.path.join(my_path, file)
print("Opening txt file " + my_dsn + " for input.")
with open(my_dsn) as da_lines:
textlines = (line.rstrip('\r\n') for line in da_lines)
for line in textlines:
print(line)
file_processed = True
if file_processed:
pass
else:
print("File not found")
print("OK, program execution has ended.")
sys.exit() # END SAMPLE CODE SNIPPET
From looking at your code, I'm assuming that you want to start at one directory, and move through each child directory, printing out the matching filename's contents if it is found.
If so, then this is way to do this with recursion:
import os
def recursive_list(path, filename):
files = os.listdir(path)
for name in files:
try:
p = os.path.join(path, name)
if os.path.isdir(p):
recursive_list(p, filename)
else:
if name == filename:
with open(p, "r") as f:
print(f.read())
except PermissionError:
pass
return
recursive_list("/home/jebby/Desktop","some_file.txt")
This will start out listing files in path. For every file that is found, if that file is a directory, then the function itself is called (Starting at the path to that folder). If filename matches the name of any file in the current directory, it will be printed (if the user has permissions for that file).
Otherwise, if you only want to read the filename from a known directory without walking down the directory tree:
import os
data_path = "/home/jebby/Desktop"
file_you_want = "filename.txt"
with open(os.path.join(data_path, file_you_want), "r") as f:
content = f.read()
print(content)
The main question would be : Do you know the location of the file?
Jebby has an answer to crawl through the directories.
Here is a solution without using "import os"
dir_fullpath = "c:/project_folder/data"
dir_path = "data"
filename = "file.txt"
try:
f = open(dir_path + "/" + filename, 'r')
# print("open " +dir_path + "\" + filename)
# data=[]
for line in f:
print (line.rstrip())
# data.append(line.rstrip())
f.close()
except IOError:
print("Fail to open" + dir_path + "\" + filename)
I need to read the contents of a file from the list of files from a directory with os.listdir. My working scriptlet is as follows:
import os
path = "/Users/Desktop/test/"
for filename in os.listdir(path):
with open(filename, 'rU') as f:
t = f.read()
t = t.split()
print(t)
print(t) gives me all the contents from all the files at once present in the directory (path).
But I like to print the contents on first file, then contents of the second and so on, until all the files are read from in dir.
Please guide ! Thanks.
You can print the file name.
Print the content after the file name.
import os
path = "/home/vpraveen/uni_tmp/temp"
for filename in os.listdir(path):
with open(filename, 'rU') as f:
t = f.read()
print filename + " Content : "
print(t)
First, you should find the path of each file using os.path.join(path, filename). Otherwise you'll loop wrong files if you change the variable path. Second, your script already provides the contents of all files starting with the first one. I added a few lines to the script to print the file path and an empty line to see where the contents end and begin:
import os
path = "/Users/Desktop/test/"
for filename in os.listdir(path):
filepath = os.path.join(path, filename)
with open(filepath, 'rU') as f:
content = f.read()
print(filepath)
print(content)
print()
os.listdir returns the name of the files only. you need to os.path.join that name with the path the files live in - otherwise python will look for them in your current working directory (os.getcwd()) and if that happens not to be the same as path python will not find the files:
import os
path = "/Users/Desktop/test/"
for filename in os.listdir(path):
print(filename)
file_path = os.path.join(path, filename)
print(file_path)
..
if you have pathlib at your disposal you can also:
from pathlib import Path
path = "/Users/Desktop/test/"
p = Path(path)
for file in p.iterdir():
if not file.is_file():
continue
print(file)
print(file.read_text())
I am trying to move text files from one folder to another by reading a path from a csv file. First I create the target folder in which I want to move my files from the existing folder. I read the existing folder path from csv file. I am working on a Windows platform.
This is my code :
import os
import csv
import shutil
#csv_filename = raw_input('Enter CSV filename:')
with open('insurance_sample.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter = ';')
header = next(readCSV)
count = 0
for row in readCSV:
dirname = "/".join(('Sorted_Program',row[1],row[4],row[3],row[7]))
#if not os.path.exists(dirname):
#os.makedirs(dirname)
path = row[10]
moveto = dirname
print path
print moveto
print os.path.isfile(path)
files = os.listdir(path)
print files
files.sort()
for f in files:
src = path + f
dst = moveto + f
break
I am getting this error after running the code:
C:\Users\Ashwin\Desktop\p\newDir\Archives\Beta\A380_1
Sorted_Program/A380/AFR/69/Flight_Test
False
Traceback (most recent call last):
File "C:\Users\Ashwin\Desktop\p\newDir\sar.py", line 19, in <module>
files = os.listdir(path)
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\Users\\Ashwin\\Desktop\\p\\newDir\\Archives\\Beta\\A380_1/*.*
Please let me know if the question is still confusing and I will try to explain in more detail.
So it seems there are two issues here:
You are creating a directory reference with / whereas windows directories require \
Your code does not prefix the new directory structure with \
Try the following modification:
dirname = "\\" + "\\".join(('Sorted_Program',row[1],row[4],row[3],row[7]))
I am trying to write a file to a specific location, to do this I wrote the following code.
The program is in a folder on an external HDD. I have used the os.path to get the current path (I think..)
the "fileName" var is = hello
the "savePath" var is = data
When I run the code I get the following error...
IOError: [Errno 13] Permission denied: 'data\hello_23-04-2014_13-37-55.csv'
Do I need to set permissions for the file before I try to write to it? If so.. how do you do this>?
def writeData(fileName, savePath, data):
# Create a filename
thisdate = time.strftime("%d-%m-%Y")
thistime = time.strftime("%H-%M-%S")
name = fileName + "_" + thisdate + "_" + thistime + ".csv"
# Create the complete filename including the absolute path
completeName = os.path.join(savePath, name)
# Check if directory exists
if not os.path.exists(completeName):
os.makedirs(completeName)
# Write the data to a file
theFile = open(completeName, 'wb')
writer = csv.writer(theFile, quoting=csv.QUOTE_ALL)
writer.writerows(data)
I get a different error (putting permission issues aside) when I try a stripped down version of what you're doing here:
>>> import os
>>> path = "foo/bar/file.txt"
>>> os.makedirs(path)
>>> with open(path, "w") as f:
... f.write("HOWDY!")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 21] Is a directory: 'foo/bar/file.txt'
Note that when you do this:
# Check if directory exists
if not os.path.exists(completeName):
os.makedirs(completeName)
...you're creating a directory that has a name that's both the path you want (good) and the name of the file you're trying to create. Pass the pathname only to makedirs() and then create the file inside that directory when you've done that.
With the below code I receive IOError: [Errno 13] Permission denied, and I know this is due to the output directory being a sub-folder of the input directory:
import datetime
import os
inputdir = "C:\\temp2\\CSV\\"
outputdir = "C:\\temp2\\CSV\\output\\"
keyword = "KEYWORD"
for path, dirs, files in os.walk(os.path.abspath(inputdir)):
for f in os.listdir(inputdir):
file_path = os.path.join(inputdir, f)
out_file = os.path.join(outputdir, f)
with open(file_path, "r") as fh, open(out_file, "w") as fo:
for line in fh:
if keyword not in line:
fo.write(line)
However, when I change the output folder to: outputdir = "C:\\temp2\\output\\" the code runs successfully. I want to be able to write the modified files to a sub-folder of the input directory. How would I do this without getting the 'Permission denied' error? Would the tempfile module be useful in this scenario?
os.listdir will return directory as well as file names. output is within inputdir so the with is trying to open a directory for reading/writing.
What exactly are you trying to do? path, dirs, files aren't even being used in the recursive os.walk.
Edit: I think you're looking for something like this:
import os
INPUTDIR= "c:\\temp2\\CSV"
OUTPUTDIR = "c:\\temp2\\CSV\\output"
keyword = "KEYWORD"
def make_path(p):
'''Makes sure directory components of p exist.'''
try:
os.makedirs(p)
except OSError:
pass
def dest_path(p):
'''Determines relative path of p to INPUTDIR,
and generates a matching path on OUTPUTDIR.
'''
path = os.path.relpath(p,INPUTDIR)
return os.path.join(OUTPUTDIR,path)
make_path(OUTPUTDIR)
for path, dirs, files in os.walk(INPUTDIR):
for d in dirs:
dir_path = os.path.join(path,d)
# Handle case of OUTPUTDIR inside INPUTDIR
if dir_path == OUTPUTDIR:
dirs.remove(d)
continue
make_path(dest_path(dir_path))
for f in files:
file_path = os.path.join(path, f)
out_path = dest_path(file_path)
with open(file_path, "r") as fh, open(out_path, "w") as fo:
for line in fh:
if keyword not in line:
fo.write(line)
If you are successful in writing to a output directory outside of the input traversing directory, then write it there first using the same code as above and then move it to a sub-directory within the input directory. You could use os.move for that.