How to open different html file in same window using Python - python

I have folder which is named as 'Doc', The Doc folder contains some sub folders each folder has '.html' file. I have to open all at time in web breowser using Python code. I opened it but the problem is, the html files is not opening in the same window with new tab. Some times each file is opening in new window. I dont know what is the exact problem. Here, is the code which I tried
import os
import webbrowser
for root, dirs, files in os.walk("Doc"):
for file in files:
if file.endswith("index.html"):
webbrowser.open_new_tab(os.path.join(root, file))

RTFM (https://docs.python.org/2/library/webbrowser.html )
webbrowser.open_new_tab(url)
Open url in a new page (“tab”) of the default browser, if possible, otherwise equivalent to open_new().
It is a best effort only, presumably depending on the browser too.

Related

Selenium how to save screenshot to specific directory in Python

I would like to take a screenshot for my selenium driver and save it to a specific directory. Right now, I can run:
driver.save_screenshot('1.png')
and it saves the screenshot within the same directory as my python script. However, I would like to save it within a subdirectory of my script.
I tried the following for each attempt, I have no idea where the screenshot was saved on my machine:
path = os.path.join(os.getcwd(), 'Screenshots', '1.png')
driver.save_screenshot(path)
driver.save_screenshot('./Screenshots/1.png')
driver.save_screenshot('Screenshots/1.png')
Here's a kinda hacky way, but it ought to work for your end result...
driver.save_screenshot('1.png')
os.system("mv 1.png /directory/you/want/")
You might need to use the absolute path for your file and/or directory in the command above, not 100% sure on that.
You can parse the file path you want to the save_screenshot function.
As your doing this already a good thing to check is that os.getcwd is the same as the location of the script (may be different if your calling it from somewhere else) and that the directory exists, this can be created via os.makedirs.
import os
from os import path
file_dir = path.join(os.getcwd(), "screenshots")
os.makedirs(file_dir, exist_ok=True)
file_path = path.join(file_dir, "screenshot_one.png")
driver.save_screenshot(file_path)
If os.getcwd is not the right location, the following will get the directory of the current script..
from os import path
file_dir = path.dirname(path.realpath(__file__))

Pasting text from clipboard to a txt file with python

I am working on a project with Selenium and I have to copy text from websites.
So far so good but I want to paste all text I copied to a *.txt file with the date and time of today.
Can somebody help me, please?
Don't think of it as 'pasting'. You have the text you want to store, you can create a new file (see python IO with files) and can write the date and time and then the string you pulled from the website with selenium.
import pyautogui
import os
import time
def sendingTextToNote():
path = "Clipboard.txt"
path = os.path.realpath(path)
os.startfile(path)
time.sleep(1)
pyautogui.hotkey('ctrl','v') #paset copied text from clipboard
pyautogui.hotkey('ctrl','s') #save the file
pyautogui.hotkey('alt','f4') #close the file and back to program
sendingTextToNote()

How do you read files on desktop with jupyter notebook?

I launched Jupyter Notebook, created a new notebook in python, imported the necessary libraries and tried to access a .xlsx file on the desktop with this code:
haber = pd.read_csv('filename.xlsx')
but error keeps popping up. Want a reliable way of accessing this file on my desktop without incurring any error response
This is an obvious path problem, because your notebook is not booted on the desktop path, you must indicate the absolute path to the desktop file, or the relative path relative to the jupyter boot directory.
You will need to enter the full path of your excel file.
First:
Open your excel file, right click on the file and click on "Copy path to clipboard".
Second:
Next paste your path in your script. Mine looks something like this:
#only using one backslash "\"
'C:\Users\...YourFileName.xlsx'
Third:
You will likely have to modify this path by adding two "\" instead of one "\" in each spot you only see one backslash.
For example, my new path would now look like this:
#using two backslashes now "\\"
'C:\\Users\\...YourFileName.xlsx'
An example of your final output will look like this:
haber = pd.read_csv('C:\\Users\\...YourFileName.xlsx')
If you are using linux
/home/(your user name)/Desktop/(your filename)
if you are on windows
C:\Users\(your user name)\Desktop\( your filename)
and if your python file is on same path where dataset file is then just give the file name with extension

Find Python files according to their content

Using File Explorer in Windows, we can find files by typing part of the file names in the Search box. With the Advance option, we even can find a file according to its content.
Is it possible to search Python files based on their content without "manually opening each file and viewing it in a viewer or editor program"? I use Jupyter Lab to create Python files.
For example, I want to find python files that contain dayfirst.
Thanks for help.
To enable content search using Windows Explorer, you can set up your Windows indexing options to include the contents of .py files. Here is a step by step guide:
https://www.howtogeek.com/99406/how-to-search-for-text-inside-of-any-file-using-windows-search/
Screenshot (for a batch file)...
(Also make sure that the location where you keep your .py files is in a location indexed by Windows.)
Take a look at pathlib.
Relevant points/example:
from pathlib import Path
p = Path('.')
files = [x for x in p.iterdir() if x.is_file()]
found_files = []
for file in files:
with file.open() as f:
for line in f:
if 'dayfirst' in line:
found_files.append(file)

Python3:Save File to Specified Location

I have a rather simple program that writes HTML code ready for use.
It works fine, except that if one were to run the program from the Python command line, as is the default, the HTML file that is created is created where python.exe is, not where the program I wrote is. And that's a problem.
Do you know a way of getting the .write() function to write a file to a specific location on the disc (e.g. C:\Users\User\Desktop)?
Extra cool-points if you know how to open a file browser window.
The first problem is probably that you are not including the full path when you open the file for writing. For details on opening a web browser, read this fine manual.
import os
target_dir = r"C:\full\path\to\where\you\want\it"
fullname = os.path.join(target_dir,filename)
with open(fullname,"w") as f:
f.write("<html>....</html>")
import webbrowser
url = "file://"+fullname.replace("\\","/")
webbrowser.open(url,True,True)
BTW: the code is the same in python 2.6.
I'll admit I don't know Python 3, so I may be wrong, but in Python 2, you can just check the __file__ variable in your module to get the name of the file it was loaded from. Just create your file in that same directory (preferably using os.path.dirname and os.path.join to remain platform-independent).

Categories

Resources