运行Python程序时出错(新手)

时间:2018-06-15 15:38:08

标签: python

如果有这样的话题,我很抱歉,但我找不到答案。所以,让我们开始吧:有人为我创建了这样的代码,但不幸的是他并没有告诉我如何运行它以及混乱本身而不是回写。我试图自己解决问题但不幸的是我的知识还没有达到这个水平。好吧,当我尝试在VSC中激活代码时,我看到了这样的错误

c:\Users\XXX\AppData\Local\Programs\Python\Python36-32\Scripts>main.py
dane.xlsx
Traceback (most recent call last):
File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\Scripts\main.py",line 21, in <module>
if __name__ == "__main__": main()
File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\Scripts\main.py", line 8, in main
xl = pd.ExcelFile(input("dane.xlsx"))
File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\io\excel.py", line 394, in __init__
self.book = xlrd.open_workbook(self._io)
File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\lib\site-packages\xlrd\__init__.py", line 116, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory:

这里我给出了2个文件的代码,也许它们有错误:      main.py

from wp import wp
import time
import pandas as pd
from selenium import webdriver

def main():
chromedriver = "chromedriver.exe"
xl = pd.ExcelFile(input("C:\Praca\dane.xlsx"))

# Print the sheet names
driver = webdriver.Chrome(executable_path=chromedriver)

# Load a sheet into a DataFrame by name: df1
df1 = xl.parse('Sheet1')
for data in df1.index:
    _wp = wp(driver,df1['Strona'][data])
    _wp._login()
    _wp._send_data(df1['Tytul'][data],df1['Tresc'][data],df1['Data'][data],df1['Logo'][data],df1['Medium'][data],df1['Link'][data],df1['LinkA'][data])


if __name__ == "__main__": main()

wp.py

from datetime import datetime
import time


def get_time():
return str(datetime.now().strftime('[%H:%M:%S] '))




class wp():

def __init__(self,driver,base_url):
    self.base_url = base_url
    self.login = "xxx"
    self.password = "xxx"
    self.driver = driver

def _login(self):
    self.driver.get(self.base_url+"/wp-login.php")

    self.driver.find_element_by_id("user_login").send_keys(self.login)
    self.driver.find_element_by_id("user_pass").send_keys(self.password)

    self.driver.find_element_by_id("wp-submit").click()


def _send_data(self,title,content,date,logo,medium,link,linka):
    self.driver.get(self.base_url+"/wp-admin/post-new.php")
    self.driver.find_element_by_id("title").send_keys(title)
    self.driver.find_element_by_id("content").send_keys(content)

    self.driver.execute_script("document.getElementsByClassName('edit-timestamp hide-if-no-js')[0].click();")

    date = date.split(".")
    if date[1][0:1] == "0": date[1] = date[1][1:2]

    self.driver.find_element_by_id("jj").send_keys(date[0])
    self.driver.find_element_by_xpath('//*[@id="mm"]/option['+date[1]+']').click()
    self.driver.find_element_by_id("aa").send_keys(date[2])


    self.driver.execute_script("document.getElementsByClassName('save-timestamp hide-if-no-js button')[0].click();")
    self.driver.execute_script("document.getElementById('set-post-thumbnail').click();")

    self.driver.execute_script("document.getElementById('media-search-input').value='"+medium+"';")

    time.sleep(1)

    self.driver.execute_script("document.getElementsByClassName('thumbnail')[0].click();")

    self.driver.execute_script("document.getElementsByClassName('button media-button button-primary button-large media-button-select')[0].click();")





    self.driver.execute_script("document.getElementById('insert-media-button').click();")


    self.driver.execute_script("document.getElementsByClassName('search')[1].value='"+logo+"';")

    time.sleep(1)

    self.driver.execute_script("document.getElementsByClassName('attachments ui-sortable ui-sortable-disabled')[1].getElementsByClassName('thumbnail')[0].click();")
    time.sleep(1)

    self.driver.execute_script("document.getElementsByClassName('media-sidebar visible')[0].getElementsByTagName('input')[1].value='"+link+"';")
    self.driver.execute_script("document.getElementsByClassName('media-sidebar visible')[0].getElementsByTagName('input')[2].value='"+link+"';")
    self.driver.execute_script("document.getElementsByClassName('media-sidebar visible')[0].getElementsByTagName('textarea')[1].value='"+link+"';")

    self.driver.execute_script("document.getElementsByClassName('attachment-display-settings')[0].getElementsByTagName('input')[0].value='"+linka+"';")
    time.sleep(1)

    self.driver.execute_script("document.getElementsByClassName('button media-button button-primary button-large media-button-insert')[0].click();")

    self.driver.execute_script("document.getElementById('publish').click();")
    time.sleep(1)

我提前道歉,并且感谢你的帮助

2 个答案:

答案 0 :(得分:3)

由于您可能正在尝试对文件路径进行硬编码而不是要求用户输入(input()),因此您应该替换

xl = pd.ExcelFile(input("C:\Praca\dane.xlsx"))

xl = pd.ExcelFile(r"C:\Praca\dane.xlsx")

答案 1 :(得分:0)

您的脚本正在查找的文件不存在。在您的示例中,您从目录&#34; c:\ Users \ XXX \ AppData \ Local \ Programs \ Python \ Python36-32 \ Scripts&#34;中执行了main.py.所以,&#34; dane.xlsx&#34;目录中不存在&#34; c:\ Users \ XXX \ AppData \ Local \ Programs \ Python \ Python36-32 \ Scripts&#34;。您可以将其移动到那里或输入dane.xlsx的完整路径,而不仅仅是文件名。