Home > Mobile >  FileNotFoundError: [Errno 2] No such file or directory: 'following.txt error accessing a file u
FileNotFoundError: [Errno 2] No such file or directory: 'following.txt error accessing a file u

Time:11-22

I have a problem. I'm trying to learn how to coding bots with Selenium. Everything ok but I can't save the follower list I got from Instagram as a txt file. Thank you for the help in advance.

def getFollowing(self):
        self.browser.get(f"https://www.instagram.com/{self.username}")
        time.sleep(3)
        self.browser.find_element_by_xpath("//*[@id='react-root']/section/main/div/header/section/ul/li[3]/a").click()
        time.sleep(3)

        dialog = self.browser.find_element_by_css_selector("div[role=dialog] ul")
        followingCount = len(dialog.find_elements_by_css_selector("li"))

        print(f"First count: {followingCount}")

        action = webdriver.ActionChains(self.browser)

        while True:
            dialog.click()
            action.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()

            newCount = len(dialog.find_elements_by_css_selector("li"))

            if followingCount != newCount:
                followingCount = newCount
                print(f"New count: {followingCount}")
                time.sleep(3)
            else:
                break

            following = dialog.find_elements_by_css_selector("li")

            followingList =[]
            i = 0
            for user in following:
                link = user.find_element_by_css_selector("a").get_attribute("href")
                followingList.append(link)
                i  = 1
                if i == followingCount 1:
                    break

            with open("following.txt", "w", encoding="UTF-8") as file:
                for item in followingList:
                    file.write(item   "\n")

elif choice == 2:
        instagram.getFollowing()
        following = open("following.txt", "r")
        for i in following:
            print(i)

ENTER YOUR COISE: 2

c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT\main.py:72: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  self.browser.find_element_by_xpath("//*[@id='react-root']/section/main/div/header/section/ul/li[3]/a").click()
c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT\main.py:75: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
  dialog = self.browser.find_element_by_css_selector("div[role=dialog] ul")
C:\Users\murat\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py:501: UserWarning: find_elements_by_* commands are deprecated. Please use find_elements() instead
  warnings.warn("find_elements_by_* commands are deprecated. Please use find_elements() instead")
First count: 12
Traceback (most recent call last):
  File "c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT\main.py", line 162, in <module>
    following = open("following.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'following.txt'

CodePudding user response:

2 things

  1. You are getting

    DeprecationWarning: find_element_by_* commands are deprecated
    

in order to resolve that, you should change all the

find_element_by_* 

command to

find_element(By.XPATH, "XPATH")

or to By.CSS_SELECTOR, By.ID, etc.. depending upon your selector.

  1. No such file

    FileNotFoundError: [Errno 2] No such file or directory: 'following.txt
    

you are using this line of code

with open("following.txt", "w", encoding="UTF-8") as file:

you are giving

following.txt

and since it's not full file path, Python will look inside the current project from where you are running your program.

Probably it is not present in the current working directory.

Please make sure it is present, also if not in the current working directory then you should give full file path.

On a windows system, it'd be something like this :

D:/FolderName/following.txt

This is just an example.

CodePudding user response:

First of all check if the file following.txt is present in the working directory.

    if followingCount != newCount:
        followingCount = newCount
        print(f"New count: {followingCount}")
        time.sleep(3)
    else:
        break  # <-- this line breaks the loop before writing the file.

PS: try reading and writing to a file in another new python script. If it works, it means in your main code, the file location is different.

CodePudding user response:

As you are using there is a minor and a major issue in your program as follows:


This error message...

FileNotFoundError: [Errno 2] No such file or directory: 'following.txt'

...implies that your program is unable to find the file following.txt in the current working directory i.e. within:

c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT

Solution

To address this error you can adopt any of the below two approaches:

  • Ensure that the required text file following.txt is placed within c:\Users\murat\Desktop\Çalışmalarım\Programlama\Exercises\InstagramBOT sub-directory.

  • You can also pass the absolute path of the required text file following.txt as follows:

    with open("c:/absolute_file_location/following.txt", "w", encoding="UTF-8") as file:
    
  • Related