I have created a script that takes a file from one folder and produces another file in another folder. This is a project to convert one format into another to be used by people who dont have strong background in informatics so I have created a folder with the script plus the input folder and the output folder. The user just need to put the input file in the input folder and take the results from the output folder.
The script works fine if I run this python script when running with visual code as well as If I run the script using the terminal ( python CSVtoVCFv3.py )
but when I convert my script in an executable with pyinstaller I found the next error.
File "CSVtoVCFv3.py", line 99, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/Users/manoldominguez/input/'
[99327] Failed to execute script CSVtoVCFv3
The code used in line 99 is:
97 actual_path = os.getcwd()
98 folder_input = '/input/'
99 input_file_name = os.listdir(actual_path+folder_input)
100 input_file_name= ''.join(input_file_name)
101 CSV_input = actual_path+folder_input+input_file_name
I have also tried this:
actual_path = (os.path.dirname(os.path.realpath('CSVtoVCFv3.py')))
So as conclusion as far as I can understand the issue is:
In these lines If I run my script I get this
'/Users/manoldominguez/Desktop/CSVtoVCF/input/'
If my script is ran with my executable I get this
'/Users/manoldominguez/input/'
os.getcwd() gives Current Working Directory - it means folder in which script was executed, but it doesn't have to be folder in which script is saved. This way you can run code in different folder and it works with files in different folder - and it can be usefull.
But if you need with files in folder where you have script then you can get this folder using
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
or
import sys
SCRIPT_PATH = os.path.dirname(os.path.realpath(sys.argv[0]))
not with 'CSVtoVCFv3.py'
And then you can join it
SCRIPT_PATH = os.path.dirname(os.path.realpath(sys.argv[0]))
folder_input = '/input/'
full_folder_input = os.path.join(SCRIPT_PATH, folder_input)
all_filenames = os.listdir(full_folder_input)
for input_file_name in all_filenames:
#CSV_input = os.path.join(full_folder_input, input_file_name)
CSV_input = os.path.join(SCRIPT_PATH, folder_input, input_file_name)
I only don't like your
input_file_name = os.listdir(actual_path+folder_input)
input_file_name= ''.join(input_file_name)
because listdir() may gives more files and then your join may create incorrect path. Better get input_file_name[0] for single file or use for-loop to work with all files in folder.
BTW: Maybe you should use sys.argv to get path as parameter and then everyone may decide where to put file.
Related
I have a requirement to be able to download attendee details from an SQL table into a CSV file.
The code below works perfectly for my local deployment, creating a .csv file in the /app/static directory which the Return statement downloads to my desktop.
When uploaded to Heroku the same code fails on the final line (Return) stating:
'FileNotFoundError: [Errno 2] No such file or directory: '/app/app/static/users1001.csv'
This suggests it has created the file on the server but is looking in the wrong place (an extra '/app'). However, no file has been created anywhere (I've checked using Heroku Run Bash). The code:
name = 'attendees' + str(name) # name = 1001 in this example
f = open('%s.csv' % name, 'w')
out = csv.writer(f)
out.writerow([ 'Date','Activity No','Activity', 'User No', 'User Name',\
'Cost','Received','Update Time'\
])
for item in Attendees.query.filter_by(club=current_user.club)\
.order_by(Attendees.activitydate.desc(),Attendees.activityname):
out.writerow([item.activitydate, item.activitynumber,\
item.activityname,item.usernum,\
item.username,item.cost,item.received,item.update_time])
path='static/'+name+'.csv' # this was necessary for the local deployment
# csv writer send to 'static' by default but
# send_file needs it stated explicitly
f.close()
return send_file(path, as_attachment=True)
My question therefore is does anyone know where this file is stored in Heroku such that I can complete the download?
One further wrinkle that I can't explain. If I run this for club 1002 (not 1001), the code works and downloads a file attendees1002.csv (this is a file I created locally and was uploaded to Heroku by GIT). You will see below that this file is actually in /app/static/temp which is really confusing.
From Heroku Run Bash:
~/app/static $ dir
1002.csv clubmanager2020.jpg code39_barcode.svg eds.jpg loading.gif ratings.csv
bwt.jpg clubmanager2020.png dump.csv eetc.jpg out.csv temp
~/app/static $ cd temp
~/app/static/temp $ dir
attendees1002.csv out.csv ratings.csv users1002.csv
Any help greatly appreciated
I've found some documentation to fix this problem. Heroku does not allow creation of files on the system except in the /tmp directory off the root directory. The problem was fixed by changing:
name = 'attendees' + str(name)
to
../../tmp/name = 'attendees' + str(name)
and
path='static/'+name+'.csv'
to
path=name+'.csv'
I'm trying to write a file with a python program. When I perform all the actions command line, they all work fine. The file is created.
When I perform the actions in a python script, the file does not exist after the script terminates.
I created a small script that demonstrates the behavior.
import os
import os.path
current_dir = os.getcwd()
output_file = os.path.join(current_dir, "locations.js")
print output_file
f = open(output_file, "w")
f.write("var locations = [")
f.write("{lat: 55.978467, lng: 9.863467}")
f.write("]")
f.close()
if os.path.isfile(output_file):
print output_file + " exists"
exit()
Running the script from the command line, I get these results:
D:\Temp\GeoMap>python test.py
D:\Temp\GeoMap\locations.js
D:\Temp\GeoMap\locations.js exists
D:\Temp\GeoMap>dir locations.js
Volume in drive D is Data
Volume Serial Number is 0EBF-9720
Directory of D:\Temp\GeoMap
File Not Found
D:\Temp\GeoMap>
Hence the file is actually created, but removed when the script terminates.
What do I need to do the keep the file?
Problem was solved by changing firewall settings.
I wrote this on my Matlab code for my MacOs:
folder_list = 'folder_list.txt';
cd(folder_paraboles)
if exist(folder_list) == 0
commande = ['ls >',folder_list];
unix(commande)
end
Does anyone can give me the corresponding line code on Matlab Windows? Thanks a lot
Rather than using unix to get the directory listing, you should just use the built-in dir or ls to get a list of files and then write them out to a file using MATLAB's built-in ability to write to files.
files = dir(pwd);
fid = fopen('output.txt', 'wt');
fprintf(fid ,'%s\n', files.name);
fclose(fid);
Using the dropbox sample code I am working on a Lua script that will run each time a new file is added (a photo in this case) to a specific folder. I see examples to iterate through a folders files to upload them each in turn. What I need is the properties that are passed to the file that will execute each time a new file is written. I'm hoping it will be passed the filename for the last file created so I can use that to upload the file directly to Dropbox or FTP site.
HI someone in japan solved your doubt as the following.
last_filepath = ""
max_mod = 0
fpath = "/DCIM/100__TSB"
for filename in lfs.dir(fpath) do
filepath = fpath .. "/" .. filename
mod = lfs.attributes( filepath, "modification" )
if mod > max_mod then
max_mod = mod
last_filepath = filepath
end
end
basically it set a directory to search for the latest file with newest modification date/time.
details r here
http://dotnsf.blog.jp/archives/1040120555.html
I have a folder which has subfolders containing 1000s of DICOM images which I want to read in from IDLE and analyze.
I have used the following code to find file paths:
import sys
print sys.path
I subsequently tried placing my folder which I want to access in these file paths, however I still can not access the files and I get the following error:
>>> fp = open(fp, 'rb')
IOError: [Errno 2] No such file or directory: 'IM-0268-0001.dcm'
I have also tried:
sys.path.insert(0, 'C:/desktop/James_Phantom_CT_Dec_16th/Images')
But this did not work for me either. Help much appreciated, very frustrated.
(using Python 2.7, 64 bit windows OS).
When opening a file, Python does not search the path. You must specify the full path to open:
d = 'C:/desktop/James_Phantom_CT_Dec_16th/Images'
fp = open(d +'IM-0268-0001.dcm',"rb")
Edit: d is the string that will hold the path so that you don't have to re-type it for each file. fp will hold the file object that you will work with. The "rb" is the way you want to open the file:
r - read
w - write with truncate
a - append
r+ - read and write
Also, if working in windows, add "b" to work with binary files. See here.