savetxt to file giving error - python

This probably has an embarrassingly easy answer, but I'm not sure what it is.
In my python code, there is a part where I want to save an array (called "stokes_columns" which is just full of floats) into a text file.
I did this fine with the following:
np.savetxt('../all_pulsars_1400list/%s_1400list.txt' % pname,stokes_columns, delimiter='\t')
The error message I get says:
no such file or directory: '~/all_pulsars_1400list/J0543_1400list.txt'
Where J0543 is the first variable to be used for the '%s'
but - I don't understand because of course there is no file called that - that is the file I am trying to create.
I've double checked the path and it exists.
Is there something obvious I'm doing wrong? Thank you.

You need to expand path to absolute path like this:
>>> import os
>>> os.path.expanduser('~/all_pulsars_1400list/J0543_1400list.txt')
'home/xxx/all_pulsars_1400list/J0543_1400list.txt'

Related

Why does Python not find/recognise a file saved in the same directory as the file where the code is written?

I am quite new to working with Python files, and having a small issue. I am simply trying to print the name of a text file and its 'mode'.
Here is my code:
f = open('test.txt','r')
print(f.name)
print(f.mode)
f.close()
I have a saved a text file called 'test.txt' in the same directory as where I wrote the above code.
However, when I run the code, I get the following file not found error:
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
Any ideas what is causing this?
I have also tried to replace the first argument above with the path of the test.txt file, but I get the same error?
open (on pretty much any operating system) doesn't care where your program lies, but from which directory you are running it. (This is not specific to python, but how file operations work, and what a current working directory is.)
So, this is expected. You need to run python from the directory that test.txt is in.
I have also tried to replace the first argument above with the path of the test.txt file, but I get the same error?
In that case, you must have mistyped the path. Make sure there's no special characters (like backslashes) that python interprets specially in there, or use the raw string format r'...' instead of just '...'.
It depend from where the python command is launched for instance :
let suppose we have this 2 files :
dir1/dir2/code.py <- your code
dir1/dir2/test.txt
if you run your python commande from the dir1 directory it will not work because it will search for dir1/test.txt
you need to run the python commande from the same directory(dir2 in the example).

Importing file (without extension) contents into python script as object

I have a file titled "passc" (no .txt, no .py, no extension at all) that contains a single line of code (its a token) similar to:
aABBccDd01234
Yes, there are no quotes or variable assignment in this file
I am trying to import this value into a python script and assign it to a variable (I think it should look something like this):
import passc
code = passc
I have tried variations of the following:
from passc import *
code = passc[0]
And I understand I'm importing a module not a direct object so it shouldn't have a subscriptable element, but I thought I might as well try it.
Without defining the value in "passc" file I can't figure out how to access the value.
Looking for suggestions on how to access this value without altering the "passc" file!
Do not complicate yourself using import passc or from passc import * of this way only we can import files with .py extension.
The fast solution to fix this problem is doing something like this:
# read file
my_file=open("passc","r")
# save code in a var
code = my_file.read()
print(code)
# aABBccDd01234
Cheers!

os.join.path does change directory

Hello I have a piece of code where I want to join a path with a picture name which I am getting from an excel file. The thing is that my code changes my path string.
src=os.path.join('C:\\Users\\ekrem\\Desktop\\Distributions\\16-17',ws.cell(row=cell.row,column=2).value)
print('src=',src)
so here my src variable should be something like this:
C:\Users\ekrem\Desktop\Distributions\16-17\26231043_1686575684735993_7548330554586169888_n.jpg
but as you see I printed the src variable and I got this
src= C:\Users\ekrem\Desktop\16-17\26231043_1686575684735993_7548330554586169888_n.jpg
here the distribution folder is missing, have you any idea why?
Thank you in advance
In this case assuming the end piece is always a single filename it might be easier to do this with f-strings. That way there is no path weirdness that happens something like this should work (Assuming you're on python 3.6+, if you aren't then do this instead):
src = f'C:\\Users\\ekrem\\Desktop\\Distributions\\16-17\\{ws.cell(row=cell.row,column=2).value}'
print(f'{source=}') # Print source with label
P.S I would recommend switching the C:\\Users\\<name> with %USERPROFILE% (so %USERPROFILE%\\Desktop\\Distributions\\16-17\\), that way if you change accounts or hard-drives this still works.
Then just do validation with:
if os.path.exists(src):
# Do stuff
else:
raise FileNotFoundError(f"{src} file could not be found")

Importing a file using pandas

Data visualisation: I want to import a file using pandas. I assigned a variable and file name given is correct but it shows a error message as file does not exist
Code: sample_data = pd.read_csv('sample_data.csv')
You're probably in the wrong working directory. Run os.getcwd() to check. If so, you can either change working directories with os.chdir() or give an absolute path to the file instead of a relative path.
If you're already in the right working directory, run os.listdir() to make sure the file is actually there.
Ok, in this case, what you will have to do is get the path file by right-clicking on the file and going to properties(Windows, don't know about Mac). Then just copy the file path and paste it instead of the file name. So for now, it should be something like this(as I don't know your file path):
sample_data = pd.read_csv('C:\Users\SVISHWANATH\Downloads\datasets')
Now, after the last folder, give in your file name. So now, it should look like this:
sample_data = pd.read_csv('C:\Users\SVISHWANATH\Downloads\datasets\sample_data.csv')
However, this will still not work as the slashes need to be the other way. Because of this, there will have to be an r before the quotes.
sample_data = pd.read_csv(r'C:\Users\SVISHWANATH\Downloads\datasets\sample_data.csv')
Now this should work as all the requirements are met.
You need to store the .csv file in a folder where the actual program is stored, otherwise you have to give a proper path to the file:
sample_data = pd.read_csv('filepath')

Exception on extracting a zipfile, after appending to it in Python

z = zipfile.ZipFile(io.BytesIO(artifact), mode='a')
z.write("test.txt",arcname=r'bin/test.txt')
z.extractall('out')
Exception:
zipfile.BadZipFile was unhandled by user code
Message: File name in directory 'bin\test.txt' and header b'bin/test.txt' differ.
The interesting thing is if I write the file to disk, and try extract it, I get a invalid file error. This is on Win 7 by the way.
the bin folder already exists in the zipfile. Full Traceback
Actually the code works well on my Mac,and I think you should let us know that the structure of the zip file or what the variable artifact is.
Here is my advice:
Use forward slashes as path separators,when you create the zip file.
Try to print a warning not raise the exception,and check out the out folder,you will find the reason,maybe the slashes or string buffer.
Also you can read this issue.
Hope this helps.

Categories