I have the following python code and it runs just fine if I run it manually. When I go to schedule it in Window's scheduler it doesn't work. Any idea why?
#Data
from datetime import date, datetime, timedelta
import os
import sys
import traceback
#Pandas and Numpy
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import numpy as np
#Set Directory
#print('\nRead in data file')
indirname = r'C:/Users/user/Desktop'
outdirname = r'C:/Users/user/Desktop'
#Read in file
data_file = os.path.join(indirname, r'File Name.xlsx')
df = pd.read_excel(data_file, sheet_name='Sheet2', skiprows=range(1))
df.to_excel('C:/Users/user/Desktop/test5.xlsx')
If your script is fine than you can check your python.exe location using where python command in your command prompt then paste the path in Program/script dilogue box then provide full path of your python file in add arguments with double quotes. e.g. "C:\user\name\folder\file.py" and empty your start in (optional)
Related
I tried running a python script using the following methods:
locally using both spyder and the command prompts windows for anaconda & cmd
then I used google colab notebook
Basically, I uploaded all the .nc files to my google drive plus an excel file with some data that are needed in the code. The code stops at some point and returns an error message saying that there is no such file although clearly the files is there.
Here is the code:
import os
from netCDF4 import Dataset
import numpy as np
import pandas as pd
os.chdir('/content/drive/MyDrive/Precipitations') # to set a new working directory
date_range=pd.date_range(start="19960101", end="20200930")
df = pd.read_excel('/content/drive/MyDrive/Precipitations/stationssa.xlsx')df1=pd.DataFrame(0,columns=df.loc[:,"NAME"],index=date_range)
for position in stations:
lat_station = df.iloc[position,1]
lon_station = df.iloc[position,2]
for day in date_range:
data = Dataset("gpcp_v01r03_daily_d"+str(day)[0:4]+str(day)[5:7]+str(day)[8:10]+".nc")
prcp=data.variables['precip'][:]
lon_data = data.variables['longitude'][:]
lat_data = data.variables['latitude'][:]
sq_diff_lat = (lat_data - lat_station)**2
sq_diff_lon = (lon_data - lon_station)**2
min_index_lat = sq_diff_lat.argmin()
min_index_lon = sq_diff_lon.argmin()
if type(prcp[0, min_index_lat, min_index_lon])==np.ma.core.MaskedConstant:
df1.loc[str(day),position] = -1
else:
df1.loc[str(day),position] = float(prcp[0, min_index_lat, min_index_lon])
df1=df1.replace(-1,np.nan) ### when the dataframe is done
df1.to_csv("Preciptations.csv") ## to export to a csv file
The link to the colab notebook is here:
<https://colab.research.google.com/drive/1q5e1nuVHfPJsKba2ImoBv456aoO9gu0s?usp=sharing
But I get this error message:
enter image description here
You need to mount your drive first, using
from google.colab import drive
drive.mount('/content/drive')
So an update, I found my compile issue was that I needed to change my notebook to a py file and choosing save as doesn't do that. So I had to run a different script turn my notebook to a py file. And part of my exe issue was I was using the fopen command that apparently isn't useable when compiled into a exe. So I redid the code to what is above. But now I get a write error when trying to run the script. I can not find anything on write functions with os is there somewhere else I should look?
Original code:
import requests
import json
import pandas as pd
import csv
from pathlib import Path
response = requests.get('url', headers={'CERT': 'cert'}, stream=True).json()
json2 = json.dumps(response)
f = open('data.json', 'r+')
f.write(json2)
f.close()
Path altered code:
import requests
import json
import pandas as pd
import csv
from pathlib import Path
response = requests.get('url', headers={'CERT': 'cert'}, stream=True).json()
json2 = json.dumps(response)
filename = 'data.json'
if '_MEIPASS2' in os.environ:
filename = os.path.join(os.environ['_MEIPASS2'], filename)
fd = open(filename, 'r+')
fd.write(json2)
fd.close()
The changes to the code allowed me to get past the fopen issue but created a write issue. Any ideas?
If you want to write to a file, you have to open it as writable.
fd = open(filename, 'wb')
Although I don't know why you're opening it in binary if you're writing text.
I am trying a project as a beginner. It is driving me nuts because I keep getting minor errors that paralyze the whole execution. Here's an error that has been plaguing me.
### SOLUTION
## 1. Introduction of dataset
import pandas as pd
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt
# This lets us see many columns in the output
pd.set_option('display.expand_frame_repr', False)
df = pd.read_csv('data.csv', index_col=0)
Error:
File "C:\ProgramData\Anaconda\Lib\site-packages\pandas\_libs\parsers.cp36-win_amd64.pyd", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
V\000~Ã\000\000ëtA¸P\000\000\000H»Ì\000H
builtins.FileNotFoundError: File b'data.csv' does not exist.
Why do I get this error even though the csv file exists?
You need to provide absolute path for the file.
For example: pd.read_csv("/path/to/the/file/data.csv", ...)
Or if you want to read the file from current directory:
import os
import sys
csv_path = os.path.dirname(os.path.abspath(sys.executable)) + '/data.csv'
df = pd.read_csv(csv_path, index_col=0)
With excel already open, at the end of some code I am simply trying to open 2 excel files using the following code. However, nothing loads!
from urllib.request import urlopen
from bs4 import BeautifulSoup
import csv
import datetime
import openpyxl
import time
openPythonQuoteLS = openpyxl.load_workbook('c:\ls_equity\quote\PythonQuoteLS.xlsx')
openQuoteLS = openpyxl.load_workbook('c:\ls_equity\quote\QuoteLS.xlsm')
If all you want to do is open files in Excel, you can just use the OS library to send the command to the OS to open the file, as Aran-Fey mentioned above.
For your specific files:
import os
# ...
os.system('start excel.exe c:\ls_equity\quote\PythonQuoteLS.xlsx')
os.system('start excel.exe c:\ls_equity\quote\QuoteLS.xlsm')
I'm trying to load a dataset with breaks in it. I am trying to find an intelligent way to make this work. I got started on it with the code i included.
As you can see, the data within the file posted on the public FTP site starts at line 11, ends at line 23818, then starts at again at 23823, and ends at 45,630.
import pandas as pd
import numpy as np
from io import BytesIO
from zipfile import ZipFile
from urllib.request import urlopen
url = urlopen("http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/ftp/10_Portfolios_Prior_12_2_Daily_CSV.zip")
#Download Zipfile and create pandas DataFrame
zipfile = ZipFile(BytesIO(url.read()))
df = pd.read_csv(zipfile.open('10_Portfolios_Prior_12_2_Daily.CSV'), header = 0,
names = ['asof_dt','1','2','3','4','5','6','7','8','9','10'], skiprows=10).dropna()
df['asof_dt'] = pd.to_datetime(df['asof_dt'], format = "%Y%m%d")
I would ideally like the first set to have a version number "1", the second to have "2", etc.
Any help would be greatly appreciated. Thank you.