File Not Found in the directory [duplicate] - python

This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 7 months ago.
File Not found when i should already copy that file to the directory
I already tried to copy that file in various folders like Include etc
import numpy as np
import collections
a=np.genfromtxt('Desktop\a.csv',delimiter=',',dtype=str)
print(a)
All the data that is in the file

\a is an escape sequence like \n. In this case, it is the BEL character. Escape the backslash (\\a), use a raw string to suppress escape code translation (r'Desktop\a.csv') or use forward slashes.
Note you are also using a relative path, meaning you have to run the code from the directory in which "Desktop" is a sub-directory. Use a full path to make sure you get it on the desktop, which will include your username like 'c:/users/garauv/desktop/a.csv' or whatever your real username is.

Related

How to get file path + file name into a list? [duplicate]

This question already has answers here:
How do I list all files of a directory?
(21 answers)
Closed 5 years ago.
I am trying to get a list of strings with the file path and the file name.
At the moment I only get the file names into the list.
Code:
hamFileNames = os.listdir("train_data\ham")
Output:
['0002.1999-12-13.farmer.ham.txt',
'0003.1999-12-14.farmer.ham.txt',
'0005.1999-12-14.farmer.ham.txt']
I would want an output similar to this:
['train_data\ham\0002.1999-12-13.farmer.ham.txt',
'train_data\ham\0003.1999-12-14.farmer.ham.txt',
'train_data\ham\0005.1999-12-14.farmer.ham.txt']
If you're on Python 3.5 or higher, skip os.listdir in favor of os.scandir, which is both more efficient and does the work for you (path is an attribute of the result objects):
hamFileNames = [entry.path for entry in os.scandir(r"train_data\ham")]
This also lets you cheaply filter (scandir includes some file info for free, without stat-ing the file), e.g. to keep only files (no directories or special file-system objects):
hamFileNames = [entry.path for entry in os.scandir(r"train_data\ham") if entry.is_file()]
If you're on 3.4 or below, you may want to look at the PyPI scandir module (which provides the same API on earlier Python).
Also note: I used a raw string for the path; while \h happens to work without it, you should always use raw strings for Windows path literals, or you'll get a nasty shock when you try to use "train_data\foo" (where \f is the ASCII form feed character), while r"train_data\foo" works just fine (because the r prefix prevents backslash interpolation of anything but the quote character).
Since you have access to the directory path you could just do:
dir = "train_data\ham"
output = map(lambda p: os.path.join(dir, p), os.listdir(dir))
or simpler
output = [os.path.join(dir, p) for p in os.listdir(dir)]
Where os.path.join will join your directory path with the filenames inside it.

Print path of an object in current working directory using Python [duplicate]

This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 6 years ago.
I have a requirement in which i have to specify a file path of an object (say 'abcd.jpg'). I am able to find the path with os module in python. But it contains '\\' as a separator. How to convert this separator to '\'.
>>> import os
>>> a = os.getcwd() + '\\' + 'abcd.jpg'
>>> a
'C:\\Python27\\abcd.jpg'
In my script i am only allowed to write the path name as 'C:\Python27\abcd.jpg'
Just do print a and you should be fine. What you are doing is actually the repr of the string.

Build a full path to Windows file in Python [duplicate]

This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 7 months ago.
How can I create a string that represents a Windows Path? I need to add a file that is generated dynamically to the end of the path as well. I have tried using raw strings but I can't seem to figure it out. Here's what I am trying to accomplish:
filename = "test.txt"
path = 'C:\Path\To\Folder\' + filename
Error I am seeing is: SyntaxError: EOL while scanning string literal
Sorry if this has been asked before I have tried looking at a few other SO questions and everyone reccomends using os.path.join but the problem there is I need to build a string, this code will not be running on a windows machine... Does that make a difference. Any help will be greatly appreciated!
You have a string literal, not a raw string. A raw string is a string literal with an r before it:
path = r'C:\Path\To\Folder\' + filename
Edit: Actually, that doesn't work because raw strings can't end with backslashes. If you still want to use a raw string, you could do one of the following:
path = r'C:\Path\To\Folder' + '\\' + filename
path = r'C:\Path\To\Folder\{}'.format(filename)
You could also double the backslashes:
path = 'C:\\Path\\To\\Folder\\' + filename
Yours didn't work because a backslash is a special character. For example, \n is not a backslash and an n; it is a new line. \' means to treat ' as a literal character instead of making it end the string. That means that the string does not end there, but you don't end it later in the line either. Therefore, when Python gets to the end of the line and you still haven't closed your string, it has an error. It is still better to use os.path.join(), though:
path = os.path.join(r'C:\Path\To\Folder', filename)
filename = "test.txt"
path = os.path.join(r'C:\Path\To\Folder',filename)
although really python works fine using / as a file separator on any OS

How to open a directory path with spaces in Python? [duplicate]

This question already has answers here:
Parsing Command line arguments in python which has spaces
(4 answers)
Closed 7 years ago.
I am trying to pass source and destination path from the command line as below
python ReleaseTool.py -i C:\Users\Abdur\Documents\NetBeansProjects\Exam System -o C:\Users\Abdur\Documents\NetBeansProjects\Release
but it is throwing error
WindowsError: [Error 3] The system cannot find the path specified: ''
due to 'Exam System' which has a space between.
Please suggest how to handle this.
Cause
Long filenames or paths with spaces are supported by NTFS in Windows NT. However, these filenames or directory names require quotation marks around them when they are specified in a command prompt operation. Failure to use the quotation marks results in the error message.
Solution
Use quotation marks when specifying long filenames or paths with spaces. For example, typing the following at the command prompt
copy c:\my file name d:\my new file name
results in the following error message:
The system cannot find the file specified.
The correct syntax is:
copy "c:\my file name" "d:\my new file name"
Note that the quotation marks must be used.

Python direct path file printing issue? [duplicate]

This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 6 months ago.
So I have a script that needs to print to a file in a different directory. I give that absolute path and python doesn't like it.
Here's where the file is located:
C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt
(I know, long path, but QT plotter makes the file names really long)
I typed:
textfile = open('C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt', 'w')
And I get this error:
IOError: [Errno 22] invalid mode ('w') or filename:
I've read that I can use relative paths, but I am unsure how to give it a relative path with so many directories to go through.
Thanks!
The problem is that python is interpreting the backslashes in your path as escape sequences:
>>> 'C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
'C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\x08uild-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
Notice that both \b and \n get translated to something else. Use a "raw" string instead:
>>> r'C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
'C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\\numbers.txt'
I believe this answer here may be of help.
Essentially, your backslashes are causing issues.

Categories

Resources