i am unable to get the Relative path in pycharm
tried all the ways but was unable to succeed
This Test1.py is present in Pages/ folder
iam trying the code
import os
import xlrd
index = 0
#wb = xlrd.open_workbook(f"./data/search_flights_input_data.xlsx")
wb = xlrd.open_workbook(f".//data//search_flights_input_data.xlsx")
sheet = wb.sheet_by_index(index)
but getting the error as
C:\Users\User\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/User/Downloads/Selenium-Python-Example-main/Selenium-Python-Example-main/pages/test1.py
Traceback (most recent call last):
File "C:/Users/User/Downloads/Selenium-Python-Example-main/Selenium-Python-Example-main/pages/test1.py", line 9, in <module>
wb = xlrd.open_workbook(f".//data//search_flights_input_data.xlsx")
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\site-packages\xlrd\__init__.py", line 111, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: './/data//search_flights_input_data.xlsx'
Process finished with exit code 1
Related
#import necessary modules
import csv
with open ("C:/iCloud Drive/Desktop/Python/o.csv") as f:
data = csv.reader(f)
for row in data:
print(row)
Heres the error message
Traceback (most recent call last):
File "/Users/noahhenninger/PycharmProjects/Refactoring/main.py", line 3, in <module>
with open ("C:/iCloud Drive/Desktop/Python/o.csv") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/iCloud Drive/Desktop/Python/o.csv'
I tried adding more specific info about the location of the file but that didn't seem to work.
I have written this Python script that checks if a file exists in a web server. And if it does exist, it copies is in the "found" directory on the local machine. However, since the web server is running Linux, and the full file path has slashes in it, it shows some errors. I have tried to solve it with "os.path" module but have been unsuccessful in my attempts.
import requests
import sys
import re
regex = re.compile(r"header(.*) footer", re.DOTALL)
data = {
"name":"user01",
"password":"secret"
}
r = requests.post('http://10.10.10.10/posts', data=data)
file = re.search(regex, r.text)
filename = sys.argv[1]
if file.group(1) != 'None':
with open('found/' + filename, 'w') as f:
f.write(file.group(1))
print("Found: ", filename)
I get the following error:
ERROR
Traceback (most recent call last):
File "/home/shit.py", line 18, in <module>
with open('found/' + filename, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'found//boot/grub/grub.cfg'
Traceback (most recent call last):
File "/home/shit.py", line 18, in <module>
with open('found/' + filename, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'found//etc/adduser.conf'
Traceback (most recent call last):
File "/home/shit.py", line 18, in <module>
with open('found/' + filename, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'found//etc/apache2/apache2.conf'
I have come up with a workaround. All I have to do is use:
filename = sys.argv[1].replace("/", "_")
It saves the files with an "_" instead of "/". For example, it saves "/etc/passwd" as "_etc_passwd".
I am trying to create a text file, using open(filename,'x'). I have tried x = 'a+', 'w+', 'w'. I am using windows 10, vs code "run python file in terminal", and python 3.8.2
import os
print("cwd",os.getcwd())
scriptpath = os.path.dirname(__file__)
filename = "test.txt" #1
#filename = scriptpath + "/test.txt" #2
#filename = r"C:\Users\harki\Documents\ALGO\ALGO-NPL\test.txt" #3
f = open(filename,'w+')
f.write("test")
f.close()
running with first filename:
PS C:\Users\harki\Documents\ALGO\ALGO-NPL> & C:/Users/harki/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py
cwd C:\Users\harki\Documents\ALGO\ALGO-NPL
Traceback (most recent call last):
File "c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py", line 11, in <module>
f = open(filename,'w+')
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
running with second filename:
PS C:\Users\harki\Documents\ALGO\ALGO-NPL> & C:/Users/harki/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py
cwd C:\Users\harki\Documents\ALGO\ALGO-NPL
Traceback (most recent call last):
File "c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py", line 11, in <module>
f = open(filename,'w+')
FileNotFoundError: [Errno 2] No such file or directory: 'c:/Users/harki/Documents/ALGO/ALGO-NPL/test.txt'
running with third filename:
PS C:\Users\harki\Documents\ALGO\ALGO-NPL> & C:/Users/harki/AppData/Local/Programs/Python/Python38/python.exe c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py
cwd C:\Users\harki\Documents\ALGO\ALGO-NPL
Traceback (most recent call last):
File "c:/Users/harki/Documents/ALGO/ALGO-NPL/test_save.py", line 11, in <module>
f = open(filename,'w+')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\harki\\Documents\\ALGO\\ALGO-NPL\\test.txt'
Edit:
Moving project outside of "Documents" folder solved the problem
I used this code to define file path and it works for me:
filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'folder/name', 'test.txt')
And then I create the file by using joblib or pickle.
In your case, it is similar to the second filename, just need to add the join() function
I have just started to learn programming and I am currently trying to read an excel file from IDLE. I'm following instruction from the book "Automate the Boring Stuff". I have successfully imported openpyxl, and thereafter, as instructed tried wb = openpyxl.load_workbook('example.xlsx') where I exchanged "example" to the actual name of the workbook. However, I get this error message:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 117, in load_workbook
archive = ZipFile(filename, 'r', ZIP_DEFLATED) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1216, in __init__
self.fp = io.open(file, filemode) FileNotFoundError: [Errno 2] No such file or directory: 'jan.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
wb = openpyxl.load_workbook('jan.xlsx')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 145, in load_workbook
raise InvalidFileException(unicode(e))
openpyxl.exceptions.InvalidFileException: [Errno 2] No such file or directory: 'jan.xlsx'
I don't understand how to solve this.
This error message simply says that python can't locate the file. When you try to open a file 'jan.xlsx', it's trying to locate it in the base directory of your code in your IDE. So say your code is in a directory called /Users/username/PycharmProjects/myCode
(I'm assuming here you are on a Mac OS as the path to your python suggests...
but jan.xlsx is in /Users/username
Since that is 2 directories up from your code directory, you can do one of two things:
Write in the absolute path to the file:
wb = openpyxl.load_workbook('/Users/username/jan.xlsx')
Use a relative path that is relative to the base project directory. Two dots in a relative path means one level up from the current directory. So if the excel file is 2 levels up, you can do:
wb = openpyxl.load_workbook('../../jan.xlsx')
I write a basic file write code -
f = open('workfile', 'r+')
f.write('0123456789abcdef')
I run the file at cmd at the same folder where I put workfile.txt file but I get the error -
IOError: [Errno 2] No such file or directory: 'workfile'
I tried also to convert to exe file with py2exe and run the exe file but nothing...
what is the problem?
thanks!
====================
the full error massage when check on compiler-
Traceback (most recent call last):
File "/home/ubuntu/workspace/.c9/metadata/workspace/2.py", line 1, in <module>
f = open('workfile', 'r+')
IOError: [Errno 2] No such file or directory: 'workfile'
rename 'workfile' to 'workfile.txt':
f = open('workfile.txt', 'r+')