Reading in a filename, manipulating it then saving it - python

Bascially I am reading in a file called Sounding, which has its name as '12142014_2345.csv' and I want to save it as
'12142014_2345_Averaged.csv'
Below is the code I have leading up to it.
basename = os.path.basename(Sounding)
basename,ext = os.path.split(basename)
with open(os.path.join(basename+'_Averaged'+ext)) as f:
w = csv.DictWriter(f, rows_1[0].keys())
w.writeheader()
And this is my error.
Traceback (most recent call last):
File "C:\Users\Bud\Desktop\OWLES RECENT\Moving Average.py", line 151, in <module>
with open(os.path.join(basename+'_Averaged'+ext)) as f:
IOError: [Errno 2] No such file or directory: u'_AveragedUIllinois_20131207Singular.csv'
I'm not exactly sure what I am doing wrong with it.

You want to use os.path.splitext (to split the extension) instead of split (that splits last path element).
And don't forget to open the file in write mode (and check your indentation):
basename,ext = os.path.splitext(basename)
with open(os.path.join(basename+'_Averaged'+ext), 'w') as f:
w = csv.DictWriter(f, rows_1[0].keys())
w.writeheader()

Looking at the file name in the trace, you can see '_Averaged' coming before the full file name. os.path.split looks for the directory divider (usually slash). I think you want os.path.splitext, which splits the string into path and extension. Since you already have just the basename, the path will be the filename without the extension.

Related

Creating text files, appending them to zip, then delete them

I am trying to get the code below to read the file raw.txt, split it by lines and save every individual line as a .txt file. I then want to append every text file to splits.zip, and delete them after appending so that the only thing remaining when the process is done is the splits.zip, which can then be moved elsewhere to be unzipped. With the current code, I get the following error:
Traceback (most recent call last): File "/Users/Simon/PycharmProjects/text-tools/file-splitter-txt.p‌​y",
line 13, in <module> at stonehenge summoning the all father. z.write(new_file)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framewo‌​rk/Versions/2.7/lib/‌​python2.7/zipfile.py‌​", line 1123, in write st = os.stat(filename) TypeError: coercing to Unicode: need string or buffer,
file found
My code:
import zipfile
import os
z = zipfile.ZipFile("splits.zip", "w")
count = 0
with open('raw.txt','r') as infile:
for line in infile:
print line
count +=1
with open(str(count) + '.txt','w') as new_file:
new_file.write(str(line))
z.write(new_file)
os.remove(new_file)
You could simply use writestr to write a string directly into the zipFile. For example:
zf.writestr(str(count) + '.txt', str(line), compress_type=...)
Use the file name like below. write method expects the filename and remove expects path. But you have given the file (file_name)
z.write(str(count) + '.txt')
os.remove(str(count) + '.txt')

Python I/O error: How do I fix this program for file paths with whitespace?

When running the codes.
file_path = raw_input("Drag the text file here: ")
file_path = file_path.strip()
file_handle = open(file_path, 'r')
for line in file_handle:
print line
Output:
Drag the text file here: /Users/user_name/Desktop/white\ space/text.txt
Traceback (most recent call last):
File "desktop/test.py", line 3, in <module>
file_handle = open(file_path, 'r')
IOError: [Errno 2] No such file or directory: '/Users/user_name/Desktop/white\\ space/text.txt'
The program runs fine for any pathname without whitespace.
The immediate fix would be removing the escaped '\\' characters, file_path.strip().replace('\\', '')
This should return /Users/user_name/Desktop/white space/text.txt which is a valid path for you to use.
Look into os.path for ways to handle pathnames.

Python Errno13 Permission Denied

I'm getting a permission denied error when trying to use glob to get a filename list and csv to open csv files for dictionary reading. Here is my code:
import csv
import glob
#Filepath variable for the CSV files
path = "C:\\Users\\josh\\Data"
for filename in glob.glob(path):
with open(filename) as csv_file:
for row in csv.DictReader(csv_file):
print row
I've tried running some file opening tests with the following code and it works perfectly. So I know I can open, read, write to this folder:
open('C:\\Users\\josh\\Data\\testing.txt', 'w').write('this is a test')
open('C:\\Users\\josh\\Data\\03142016.csv')
Lastly, here is the full traceback if that helps:
Traceback (most recent call last):
File "<ipython-input-10-49215e5eb704>", line 1, in <module>
runfile('C:/Users/josh/.spyder2/csvparser2.0.py', wdir='C:/Users/josh/.spyder2')
File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile
execfile(filename, namespace)
File "C:/Users/josh/.spyder2/csvparser2.0.py", line 41, in <module>
with open(filename) as csv_file:
IOError: [Errno 13] Permission denied: 'C:\\Users\\josh\\Data'
Any ideas? Thank you for your help.
Right now you're doing:
path = "C:\\Users\\josh\\Data"
for filename in glob.glob(path):
glob.glob() on only a pathname without any special globbing characters (*, ?, etc) will just return that pathname. In this case the directory name, which you're not allowed to open() since that doesn't make a lot of sense.
You probably intended to write:
glob.glob(path + '\\*'):
or:
glob.glob(path + '\\*.csv'):
Bonus tip
You already figured out that using open('C:\\Users\\josh\\Data\\03142016.csv') works fine, which is a good first step in solving the problem. You could have found out the error by using basic "printf-debugging":
path = "C:\\Users\\josh\\Data"
for filename in glob.glob(path):
print(filename)
with open(filename) as csv_file:
[.. trim ..]
This would have printed out C:\\Users\\josh\\Data, and not C:\\Users\\josh\\Data\\03142016.csv :-)
Lesson: when in doubt, print() out as many values as you can, and check if they're what you expect ;-)

How do I change/choose the file path in Python?

I'm trying to access a .txt file in Python and I can't figure out how to open the file. I ended up copying the contents into a list directly but I would like to know how to open a file for the future.
If I run this nothing prints. I think it's because Python is looking in the wrong folder/directory but I don't know how to change file paths.
sourcefile = open("CompletedDirectory.txt").read()
print(sourcefile)
The file CompletedDirectory.txt is probably empty.
If Python could not find the file, you would get a FileNotFoundError exception:
>>> sourcefile = open("CompletedDirectory.txt").read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'CompletedDirectory.txt'
Note that using read() in this way is not recommended. You're not closing the file properly. Use a context manager:
with open("CompletedDirectory.txt") as infile:
sourcefile = infile.read()
This will automatically close infile on leaving the with block.
You can get the current working directory:
import os
os.getcwd()
Then just concat it with the file container directory
os.path.join("targetDir", "fileName")

Python IOError: [Errno 2] from recursive directory call

The code below is part of a program I am writing that runs a method on every .py, .sh. or .pl file in a directory and its folders.
for root, subs, files in os.walk("."):
for a in files:
if a.endswith('.py') or a.endswith('.sh') or a.endswith('.pl'):
scriptFile = open(a, 'r')
writer(writeFile, scriptFile)
scriptFile.close()
else:
continue
When writing the program, it worked in the directory tree I wrote it in, but when I moved it to another folder to try it there I get this error message:
Traceback (most recent call last):
File "versionTEST.py", line 75, in <module>
scriptFile = open(a, 'r')
IOError: [Errno 2] No such file or directory: 'enabledLogSources.sh'
I know something weird is going on because the file is most definitely there...
You'll need to prepend the root directory to your filename
scriptFile = open(root + '/' + a, 'r')
files contains only the file names, not the entire path. The path to the file can be obtained by joining the file name and the root:
scriptFile = open(os.path.join(root, a), "r")
You might want to have a look at
https://docs.python.org/2/library/os.html#os.walk

Categories