My teacher put up a large data in the school server and gave me a piece of code to open up the files. Below is my code:
import sys
sys.path.append("/Data/Data123/.local/lib/python2.7/site-packages")
from cloud.common import get_tile
from scipy.io import netcdf_file as copen
from scipy import interpolate
import matplotlib
from matplotlib import pyplot as pp
import matplotlib.cm as cm
import numpy
path = '/Data/a'
filenames = ['Hello.nc']
But when I run the code, it says there is "No such file or directory :'Hello.nc'
I am sure files are in that directory. So I want to ask, what did I do wrong?
Or am I not even collecting to the directory?
Thanks
It would be very difficult to answer this question without knowing what files are on the server and also looking at the code that actually connects, and attempts to retrieve the file; but I can provide you with this suggestion.
Use os.path instead of declaring a literal path string. For example:
Change:
path = '/Data/a'
To:
import os
path = os.path.join(r'/Data', 'a')
Then, when you concatenate path with a filename, use os.path.join again:
os.path.join(path, filenames[0]) # Modify to fit your filenames loop accordingly
The problem may be in the way you combine path with a filename. If this does not help, please add more code or double check the server to make sure the file is there.
Related
I'm trying to move a file but it won't work. It keeps giving the error "Errno 2: File or directory does not exist". The code is shown below
import shutil
original = '%userprofile%/Desktop/Test'
New = '%userprofile%/Desktop/Test2'
shutil.move(original, New)
if anyone has any advice on how to solve this please help me.
You can use os.path.expandvars to expand the %userprofile% variable and the resultant path can be passed into the shutil.move API.
>>> help(os.path.expandvars)
Help on function expandvars in module ntpath:
expandvars(path)
Expand shell variables of the forms $var, ${var} and %var%.
Unknown variables are left unchanged.
ie,
shutil.move(os.path.expandvars(original), os.path.expandvars(New))
If %userprofile% is the only variable you need, it can be done with pathlib.Path.expanduser as such:
import shutil
from pathlib import Path
original = Path('~/Desktop/Test').expanduser()
New = Path('~/Desktop/Test2').expanduser()
shutil.move(original, New)
I'm new to python and trying to learn a few exercises via colab. I want to import a CSV file that I saved to my desktop. Unfortunately, I keep getting a "cannot find file" error message. Not sure what I'm doing wrong.
Here's my code:
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
mpg = pd.read_csv(r"C:\Users\micha\OneDrive\Desktop\mpg2018.csv.csv")
I tried to change csv.csv to csv.txt or leave as just .csv but nothing works. Any help would be great!
Foward slashes will work in this function.
mpg = pd.read_csv("C:/Users/micha/OneDrive/Desktop/filename.csv")
Since you've imported "os", you could also use path.join()
p = os.path.join("C:\\", "Users", "micha", "OneDrive", "Desktop", "a.csv")
mpg = pd.read_csv(p)
Also, that file format repetition within the name seems unnecessary. It may lead to more confusion.
You are using colab.research.google.com, which lives in its own cloud world and has no idea of what files are on your personal machine. However, if you
from google.colab import files
files.upload()
It will open a nice dialog box which will allow you to find the file in the usual way.
In files section you find Upload button click on it add your file in colab
now simply import pd.read_csv(path)
how you find path: left click on csv file then select copy path and paste in place of path in pd.read_csv()
I am trying to develop a CNN for image processing. I have about 130 gigs stored on a separate drive on my comp, and I'm having trouble navigating a simple python search program to search through that specified directory. Im trying to have it find a bunch of random XML files scattered in a host of sub-directories/sub-directories/subs on that drive. How do I specify for just this one python program the directory it should be searching in, keeping it only to the context of the program?
Ive tried setting a variable Path = "B:\\MainFolder\SubFolder" and using os.walk, but it makes it through the first directory then stops.
can you try the following:
import os
import glob
base_dir = 'your/start/sirectory'
req_files = glob.glob(os.path.join(base_dir, '**/*.xml'), recursive=True)
Jeril and Eduardo, thank you for the help. i took a shot at pathlib and it worked. idk what was up with my glob code, looked basically the same as yours Jeril:
import glob, os
filelist = []
from pathlib import Path
for path in Path('B:\\CTImageDataset\LIDC-IDRI').rglob('*.xml'):
filelist.append(path.name)
print(filelist)
Worked great, thanks again
I am trying to load a file from the following path:
path = 'C:/Users/Aman/Alzheimer/test-network/ADNI1_Complete_1Yr_1.5T/ADNI/002_S_0685/MPR__GradWarp__B1_Correction__N3__Scaled/2006-07-06_10_36_49.0/ADNI_002_S_0685_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070216235850690_S16309_I40683.nii'
However, the file is not loading. If I move the file 1 level up and change the path to
path = 'C:/Users/Aman/Alzheimer/test-network/ADNI1_Complete_1Yr_1.5T/ADNI/002_S_0685/MPR__GradWarp__B1_Correction__N3__Scaled/2006-07-06_10_36_49.0/S16309/ADNI_002_S_0685_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070216235850690_S16309_I40683.nii'
it loads.
The only difference is the S13893 folder.
I have the following code:
import nibabel as nib
import matplotlib.pyplot as plt
from scipy.misc import imsave as imsave
path = 'C:/Users/Aman/Alzheimer/test-network/ADNI1_Complete_1Yr_1.5T/ADNI/002_S_0685/MPR__GradWarp__B1_Correction__N3__Scaled/2006-07-06_10_36_49.0/ADNI_002_S_0685_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070216235850690_S16309_I40683.nii'
im = nib.load(path).get_data()
print(im.shape)
any help would be great.
I tried using r in the beginning of the path, however it didn't work.
Also, I tried using \\?\ which again didn't work.
As a final resort, I found out that it was actually an issue with the path length and shortened the path by removing commonalities in the path for all files and it worked fine.
Thanks for all the help.
However, this is just a workaround I performed and the suggested edits/changes didn't work for me.
In Windows you should try with:
path = r'C:/Users/Aman/Alzheimer/test-network/ADNI1_Complete_1Yr_1.5T/ADNI/002_S_0685/MPR__GradWarp__B1_Correction__N3__Scaled/2006-07-06_10_36_49.0/ADNI_002_S_0685_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070216235850690_S16309_I40683.nii'
I've succeeding in importing a single file, but the file calls other files and I get an error. So I'm trying to import and entire folder. I would prefer not to import each file one by one since I know it must be possible to import the whole folder. Here is the syntax I used to import a file:
import importlib.machinery
import os
temp_directory2 = '/Users/me/PycharmProjects/inference_engine2/inference2/ancient/temp.py'
temp_directory = '/Users/me/PycharmProjects/inference_engine2/inference2/Proofs/main_loop.py'
main_directory = '/Users/me/PycharmProjects/inference_engine2/inference2/Proofs/'
b = os.path.exists(temp_directory)
loader = importlib.machinery.SourceFileLoader('temp', temp_directory)
handle = loader.load_module('temp')
You can add the path to the list sys.path at the beginning of your file, like so:
import sys; sys.path.insert(0, r'C:/Users/me/PycharmProjects/inference_engine2/inference2/Proofs')
Note since you are inserting the path at the beginning of the list, this is the first place python will go to look for a module.
Convert it to a package using __init__.py. more here : https://docs.python.org/2/tutorial/modules.html