This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 4 years ago.
Can anyone explain how to work with paths with python on windows.
the path is given as parameter
path = 'C:\Data\Projects\IHateWindows\DEV_Main\Product\ACSF\Dev\DEV\force.com\src\aura'
Im trying to get the content of the folder but is not a valid path
is reading the path as:
'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\x0corce.com\\src\x07ura'
trying some solutions...
for f in listdir(path.replace("\\", "\\\\")): print (f)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\\\Data\\\\Projects\\\\Prd_Development\\\\DEV_Main\\\\Product\\\\ACSF\\\\Dev\\\\DEV\x0corce.com\\\\src\x07ura'
for f in listdir(path.replace("\\", "/")): print (f)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:/Data/Projects/Prd_Development/DEV_Main/Product/ACSF/Dev/DEV\x0corce.com/src\x07ura'
EDIT:
Solution
path = path.replace("\a", "\\a").replace("\f", "\\f")
https://docs.python.org/2.0/ref/strings.html
A single Backslash is a escape character in Python. Therefore you might need to use double Backslashes or a forward slash:
path = 'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\\force.com\\src\\aura'
or
path = 'C:/Data/Projects/IHateWindows/DEV_Main/Product/ACSF/Dev/DEV/force.com/src/aura'
Related
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 7 months ago.
I want to work with paths in Windows in Python 3.3, but I have an error:
FileNotFoundError: [Errno 2] No such file or directory:
'E:\\dir\\.project'
The problem is the double backslash. I read the solution using r.
def f(dir_from):
list_of_directory = os.listdir(dir_from)
for element in list_of_directory:
if os.path.isfile(os.path.join(dir_from, element)):
open(os.path.join(dir_from, element))
f(r'E:\\dir')
I have this error again
FileNotFoundError: [Errno 2] No such file or directory:
'E:\\dir\\.project'
os.path.normpath(path) doesn't solve my problem.
What am I doing wrong?
If you are using a raw-string, then you do not escape backslashes:
f(r'E:\dir')
Of course, this problem (and many others like it) can be solved by simply using forwardslashes in paths:
f('E:/dir')
Changing '\\' for '/' worked for me. I created a directory named 'a' in C:/ for this example.
>>> (Python interpreter)
>>> import os
>>> os.path.isdir('C:/a/)')
>>> True
>>> os.path.isfile('C:/a/)')
>>> False
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
How do you properly determine the current script directory?
(16 answers)
Closed 6 months ago.
I am trying to write a program in python where it reads a list of usernames from a file and sends a request to instagram to see if the username is available or not. Then it should prompt a message saying "THIS USERNAME IS AVAILABLE" or otherwise "THIS USERNAME IS TAKEN". I am having an error when trying to read my file that states 'FileNotFoundError: [Errno 2] No such file or directory: 'list.txt', even though the file is in my folder where the program resides.
I am also fairly new to python.
Here is my code currently:
import requests
filepath = 'list.txt'
separator = "\n" #Every time you use a newline it detects it as a new user
list = open(filepath, "r").read().split(separator)
notTaken = []
for i in list :
response = requests.get("https://instagram.com/" + i + "/")
if response.status_code == 404 :
notTaken.append(i)
It worked in my case. My list.txt was in the same directory as my python file
import requests
with open('list.txt','r') as f:
data = f.read().split("\n")
for i in data:
pass
#...(write the rest of the code)
Use the absolute directory for that file i.e /home/directory/file_name if you are using window else use c:\\\\directory\\file_name
this is if the file is not in the current working directory of the terminal. If you are in the same directory of the file in the terminal, use./file_name
Since the file is in the same directory as your program, your code will only work if the current working directory is also that same directory. If you want to run this program from anywhere, you need a way to find that directory.
When python executes a script or module it adds a __file__ variable giving the path. (Extension modules and modules in compressed packages may not have this variable). The __file__ path can be relative to the current working directory or absolute. The pathlib library gives us a handy method for working with paths. So,
from pathlib import Path
separator = "\n" #Every time you use a newline it detects it as a new user
filename = 'list.txt'
# get absolute path, then parent directory, then add my file name
filepath = Path(__file__).resolve().parent/filename
mylist = filepath.open().read().split(separator)
Since a "\n" newline is used to demark the file, you don't need to split it yourself. You could do this instead
mylist = filepath.open().readlines()
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 4 years ago.
I want to iterate over a number of files in the L: drive on my computer in a folder called 11109. This is my script:
for filename in os.listdir('L:\11109'):
print(filename.split('-')[1])
However the error message comes back as :
File "L:/OMIZ/rando.py", line 12, in <module>
for filename in os.listdir('L:\11109'):
FileNotFoundError: [WinError 3] The system cannot find the path specified:
'L:I09'
Its reading the L:\ 11109 fine but the error message said the path specified is L:I09?
You need to use raw strings or escape the backslash, otherwise \111 is resolved to I:
a = 'L:\11109'
print(a) # shows that indeed 'L:I09'
b = r'L:\11109'
print(b) # prints 'L:\11109'
c = 'L:\\11109' # will be understood correctly by open()
To solve this you can do like this
for filename in os.listdir('L:/11109'):
print(filename.split('-')[1])
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.
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.