Navigating between directories in python - python

I run the following sequence of commands. The aim was moving between various directories and folders based on the original directory (so it does not need to be apriori coded):
path=os.getcwd()
os.chdir('..')
path2=os.getcwd()
path3=path2+('\\mydir')
os.chdir('path3')
As a result I get an error:
The system cannot find the file specified: 'C:\\work_folder\\mydir'
The directory C:\work_folder\mydir exists in the system, so the problem is in my opinion in missinetrpretation of '\'.
Thus I tried to do the following:
path3=path3.replace(r'\\',r'\')
Again I am getting error:
SyntaxError: EOL while scanning string literal
I will apreciate any help in overcoming this problem. Thank you

In python, instead of using \s in your directories, you can use /s instead and it'll still work.

You can not use a single un-escaped backslash (\) in a raw string. Change the line to:
path3 = path3.replace(r'\\', '\\')
This will solve your second error (SyntaxError: EOL while scanning string literal).

Related

how to read an image with opencv using path variable in Python

So I have a variable that holds the image path as a string. but I get the below error while trying to read the image using the path variable. The path variable is coming from another function that calls this code.
Error:
can't open/read file: check file path/integrity
My code:
path = "D://dev//py_scripts//SS1.jpg"
image=cv2.imread(path)
Tried several solutions but ended up with same error.
Double forward slashes make the path invalid.
Use single forward slashes. That is commonly tolerated on Windows, which natively wants backslashes.
Double backslashes are only what you see. They're actually single backslashes in the path/string, but the string syntax requires escaping backslashes (and other stuff) by a preceding backslash, so that's why one has to use double backslashes in most string literals...
Python has "r-strings" ("raw" strings). In a raw string, even backslashes are taken literally, and nothing is escapable (a matching quote character ends the string, always).
"D:/dev/py_scripts/SS1.jpg"
r"D:\dev\py_scripts\SS1.jpg"
"D:\\dev\\py_scripts\\SS1.jpg"
import cv2
path1= "SS1.jpg"
#sometimes doesnt work depends on the compiler. If its on sub folder #py_scripts\SS1.jpg
or
path2= "d:\dev\py_scripts\SS1.jpg"
image = cv2.imread(path2)
If you want to use path1 method, and set it to work 100%
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
I would suggest changing the format of the path as follows:
import cv2
#Several options
path = r"D:\dev\py_scripts\SS1.jpg"
path = r"D:/dev/py_scripts/SS1.jpg"
path = "D:/dev/py_scripts/SS1.jpg"
image=cv2.imread(path)
But based on the error you are getting, you need to check the format of the image and its integrity, as the error says. That means you must be able to open the image without issues on an editor like Windows Photos to check if the file is not corrupted.

Open an external file in python

x = open('Homework','r')
print(x.name)
x.close()
I got this error when I run the code.
File "C:/Users/LENOVO/Desktop/pythonhome/tobechanged.py", line 16, in <module>
x = open('Homework','r')
FileNotFoundError: [Errno 2] No such file or directory: 'Homework'
SO I tried to type the full path
x = open('C:\Users\LENOVO\Desktop\pythonhome','r')
print(x.name)
x.close()
I got an Unicode error.
btw I'm using windows.
As the comments mentioned, it's usually good to type out the full path to the file, because running a script in IDLE, for example, can cause Python to search for the file in a directory that you are not intending. The reason you got the Unicode error is because you are using a special character, the backslash (\) which starts something known as an escape sequence. Escape sequences allow coders to specify special characters, like the newline character: \n. You can read more about these in Python's docs here
You have to either use a raw string (a string preceded with r, like this r'C:\Users\...'), or escape these characters with double backslashes, like this: C:\\Users\\....
Additionally, you need to specify the extension for the Homework file, otherwise the file system won't be able to find the file you are referring to, resulting in the FileNotFoundError you encountered. As #tdelaney mentioned, these extensions may be hidden by default in Windows Explorer.
Also, the recommended way in Python to open files is using the with statement, as this handles closing the object for you. Here is a sample (assuming that the extension of the Homework file is .txt):
with open('C:\\Users\\LENOVO\\Desktop\\pythonhome\\Homework.txt', 'r') as x:
print(x.name)
It is because you are forgetting the extension to the file (the ending of it). For example, if you have a text file that is named Homework, you would include it in like this
open(r'Homework.txt','r')
For this example, it must be in the same directory as your script. If you wanted to open a file outside of your scripts directory, you would have to find the full path of it. Here is an example of the Homework.txt file in my downloads folder.
open(r'C:\Users\USER\Downloads\Homework.txt','r')
You can also see in this code I use an r infront of the path. This tells Python the expression is a raw string and escape sequences are not parsed.

Can't read a .CSV file in Spyder (Python 3.6) and it's not a path or character issue

I'm new in python, I'm learning it so, please, try to be simple when answer me =) thx since now! I'm using Spyder (Python 3.6) and trying to run these lines:
import pandas as pd
df=pd.read_csv(r'Legumes.csv')
df
But, I'm having every possible kind of error, and trust me, I read and tried a lot of solutions but, none seemed to fix my problem.
First, the file "Legumes" does exist, and it is in the same folder than the spyder file (.py one).
1-I inicially tried run the lines using a relative path, since the two files are in the same folder, spyder returned me this error:
FileNotFoundError: File b'Legumes.csv' does not exist
2-Then I tried use a full path, but, I've got this error:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
3-Then, I tried insert the "r", before the path, so python would understand that this string means a path, and, I've got this:
FileNotFoundError: File b'Legumes.csv' does not exist
Note: I made sure of using no invalid caracters in the path and in the file (.csv), like ´, ^, or ~. I also tried use "r" in full and relative path (as shown in my paste above), the error message was the same.
4-I tried rename the files and run, nothing changed. Tried also re-load .py file to discart some location issues, nothing again...
The most irritating thing is, it works only for the first time, I wrote it last friday and worked just fine, I just saved and closed. Today when I got back to work it just don't run anymore!! I'm blowing up with this!!!!
Your path needs to be absolute
On Mac / Linux
‘/path/to/legumes.csv’
Pro-Tip: You can get the full path by right clicking on the file while holding option key, then select “copy legumes.csv as pathname”
On Windows
r’C:\\path\to\legumes.csv’
The reason we have the r is because we want it to be interpreted as a raw string, not a string that we are trying to escape with \ characters
Some of these escape characters are \t for a tab, \n for a new line and so forth. Any another Other Unrecognizable Escape Character will throw you that error
codec can't decode bytes
Pro-Tip: you can get the full path by right clicking on the file while holding shift key and then selecting “copy as path”
There are a number of different formats you could use for the path to the file, including the following:
# Absolute path
dataframe = pd.read_csv(r'd:\development\data.csv')
dataframe = pd.read_csv('d:\development\data.csv')
dataframe = pd.read_csv('d:/development/data.csv')
If you'd like to use a relative path, that's also possible.
# Relative path
dataframe = pd.read_csv('data.csv')
However, it seems that the relative path is not relative to the current file, but relative to the working directory. In Spyder 5, you can set that typing/browsing with the directory selection tool at the top of the IDE.

os.listdir works in console but fails in a script

My script uses os.listdir to get a list of directories to use later for batch analysis.
when running
mypath='//home//user//Documents//data'
datalist=os.listdir(mypath)
in console, I get the correct answer.
However, when I use the same code as a part of the script, python falls over on the datalist line
FileNotFoundError: [Errno 2] No such file or directory: '//home//user//Documents//data//'
Quoted from here:
The r'..' string modifier causes the '..' string to be interpreted
literally. That means, r'My\Path\Without\Escaping' will evaluate to
'My\Path\Without\Escaping' - without causing the backslash to escape
characters. The prior is equivalent to 'My\\Path\\Without\\Escaping'
string, but without the raw modifier.
And there is an explanation with a simple example at this link.
So use
mypath = r'\home\user\Documents\data'

Cannot read in files

I have a small problem with reading in my file. My code:
import csv as csv
import numpy
with open("train_data.csv","rb") as training:
csv_file_object = csv.reader(training)
header = csv_file_object.next()
data = []
for row in csv_file_object:
data.append(row)
data = numpy.array(data)
I get the error no such file "train_data.csv", so I know the problem lies with the location. But whenever I specify the pad like this: open("C:\Desktop...etc) it doesn't work either. What am I doing wrong?
If you give the full file path, your script should work. Since it is not, it must be that you have escape characters in your path. To fix this, use a raw-string to specify the file path:
# Put an 'r' at the start of the string to make it a raw-string.
with open(r"C:\path\to\file\train_data.csv","rb") as training:
Raw strings do not process escape characters.
Also, just a technical fact, not giving the full file path causes Python to look for the file in the directory that the script is launched from. If it is not there, an error is thrown.
When you use open() and Windows you need to deal with the backslashes properly.
Option 1.) Use the raw string, this will be the string prefixed with an r.
open(r'C:\Users\Me\Desktop\train_data.csv')
Option 2.) Escape the backslashes
open('C:\\Users\\Me\\Desktop\\train_data.csv')
Option 3.) Use forward slashes
open('C:/Users/Me/Desktop/train_data.csv')
As for finding the file you are using, if you just do open('train_data.csv') it is looking in the directory you are running the python script from. So, if you are running it from C:\Users\Me\Desktop\, your train_data.csv needs to be on the desktop as well.

Categories