This is known as a 'known plaintext' attack. All modern encryption algorithms are designed to be resistant to this type of attack.
Many types of files contain known plaintext. For example, PDF documents begin with the header '%PDF'. Other information in most PDF documents is easy to guess, such as embedded fonts, formatting info, etc. If known plaintext attacks were possible, then it would be possible to derive the key used to encrypt any PDF document, in the manner that you describe, based on this known content. The same would apply to many other types of files as well.
Appended 5/10/2020 based on the comments below following this answer and other answers to this question:
Very rarely is asymmetric encryption actually used to encrypt large files. Instead, most asymmetric encryption implementations use hybrid encryption, where a symmetric key is generated, then this key is used to encrypt the files using symmetric encryption (e.g. AES), then the symmetric key is encrypted using asymmetric encryption with the public key, and stored along with the ciphertext. See https://crypto.stackexchange.com/questions/32692/what-is-the-typical-block-size-in-rsa.
So, even when asymmetric encryption (e.g. RSA) is used, the underlying encryption is most likely done using symmetric encryption (e.g. AES). Most modern AES implementations are highly resistant to known plaintext attacks - even if the entire file consists of known plaintext. For example, AES-CBC uses a random initial vector which is XOR'd with the first block of plaintext. This has the effect of completely randomizing the plaintext before it is encrypted. Then, this process is repeated for all subsequent blocks.
So, a known plaintext attack is highly unlikely if hybrid encryption is used - but you make a good point that the underlying (symmetric) key that is used to encrypt the files is in memory for the entire time that the encryption is taking place.